0

0

函数 - JavaScript 挑战

花韻仙語

花韻仙語

发布时间:2024-11-01 16:10:17

|

402人浏览过

|

来源于dev.to

转载

函数 - javascript 挑战

您可以在 repo github 上找到这篇文章中的所有代码。

JavaScript原生数组函数探索
JavaScript原生数组函数探索

JavaScript原生数组函数探索

下载

功能相关的挑战


参数和参数

/**
 * @param {function} fn
 * @return {number}
 */

function functionlength(fn) {
  return fn.length;
}

// usage example
function myfunction(a, b, c) {
  console.log(a, b, c);
}

console.log(functionlength(myfunction)); // => 3

/**
 * @param {...any} args
 * @return {number}
 */

function numofarguments(...args) {
  // return args.length;
  return arguments.length;
}

// usage example
console.log(numofarguments(1, 2, 3, 4, 5)); // => 5
console.log(numofarguments()); // => 0

撰写

/**
 * @param {...functions} fns
 * @return function
 */

function compose(...fns) {
  return function (x) {
    let result = x;

    for (const fn of fns.reverse()) {
      result = fn(result);
    }

    return result;
  };
}

// usage example
const add1 = (num) => num + 1;
const double = (num) => num * 2;
const subtract10 = (num) => num - 10;

const composedfn = compose(subtract10, double, add1);
console.log(composedfn(3)); // (3 + 1) * 2 - 10 => -2

柯里化

/**
 * @param {function} fn
 * @return {function}
 */

function curry(fn) {
  return function curried(...args) {
    if (args.length >= fn.length) {
      return fn.apply(this, args);
    }

    return curried.bind(this, ...args);
  };
}

// usage example
// single parameter case
function add(a, b) {
  return a + b;
}

const curriedadd = curry(add);
console.log(curriedadd(3)(4)); // => 7
const alreadyaddedthree = curriedadd(3);
console.log(alreadyaddedthree(4)); // => 7

// fixed parameters case
function addtwo(a, b) {
  return a + b;
}

const curriedaddtwo = curry(addtwo);
console.log(curriedaddtwo(3, 4)); // => 7
console.log(curriedaddtwo(3)(4)); // => 7
const alreadyaddedthreeb = curriedadd(3);
console.log(alreadyaddedthreeb(4)); // => 7

//-------------------------------------------

/**
 * @param {function} fn
 * @return {function}
 */

function curry(fn) {
  return function curried(...args) {
    const bindfn = curried.bind(this, ...args);
    bindfn[symbol.toprimitive] = () => fn.call(this, ...args);

    return bindfn;
  };
}

// usage example
// non-fixed parameters case
function multiply(...numbers) {
  return numbers.reduce((a, b) => a * b, 1);
}

const curriedmultiply = curry(multiply);
const multiplybythree = curriedmultiply(3);
console.log(multiplybythree); // => 3
console.log(multiplybythree(4)); // => 12

const multiplybyfifteen = multiplybythree(5);
console.log(multiplybyfifteen); // => 15
console.log(multiplybyfifteen(2)); // => 30

console.log(curriedmultiply(1)(2)(3)(4)); // => 24
console.log(curriedmultiply(1, 2, 3, 4)); // => 24

备忘录

/**
 * @param {function} func
 * @return {function}
 */

function memoize(fn) {
  const cache = new map();

  return function (arg) {
    if (cache.has(arg)) {
      return cache.get(arg);
    }

    const result = fn.call(this, arg);
    cache.set(arg, result);

    return result;
  };
}

// usage example
function expensivefunction(n) {
  console.log("computing...");
  return n * 2;
}

// create a memoized version of the function.
const memoizedexpensivefunction = memoize(expensivefunction);

// first call (computes and caches the result).
console.log(memoizedexpensivefunction(5)); // => computing... 10

// second call with the same argument (returns the cached result).
console.log(memoizedexpensivefunction(5)); // => 10

// third call with a different argument (computes and caches the new result).
console.log(memoizedexpensivefunction(10)); // => computing... 20

// fourth call with the same argument as the third call (returns the cached result).
console.log(memoizedexpensivefunction(10)); // => 20

// ----------------------------------------
// when parameters could be array
/**
 * @param {function} fn
 * @return {function}
 */

function memoize(fn) {
  const cache = new map();

  return function (...args) {
    const key = json.stringify(args);

    if (cache.has(key)) {
      return cache.get(key);
    }

    const result = fn.call(this, ...args);
    cache.set(key, result);

    return result;
  };
}

// usage example
function expensivemul(a, b) {
  console.log("computing...");
  return a * b;
}

// create a memoized version of the function.
const memoizedexpensivemul = memoize(expensivemul);

// first call (computes and caches the result).
console.log(memoizedexpensivemul(3, 7)); // => computing... 21

// second call with the same argument (returns the cached result).
console.log(memoizedexpensivemul(3, 7)); // => 21

// third call with a different argument (computes and caches the new result).
console.log(memoizedexpensivemul(5, 8)); // => computing... 40

// fourth call with the same argument as the third call (returns the cached result).
console.log(memoizedexpensivemul(5, 8)); // => 40

部分的

/**
 * @param {Function} fn
 * @param {any[]} args
 * @returns {Function}
 */

function partial(fn, ...args) {
  return function (...restArgs) {
    const copyArgs = args.map((arg) => {
      return arg === partial.placeholder ? restArgs.shift() : arg;
    });

    return fn.call(this, ...copyArgs, ...restArgs);
  };
}

partial.placeholder = Symbol();

// Usage example
const func = (...args) => args;
const func123 = partial(func, 1, 2, 3);
console.log(func123(4)); // => [1, 2, 3, 4]

参考

  • 伟大的前端
  • 参数对象 - mdn
  • 参数 - mdn
  • 函数组合(计算机科学)- wikipedia.org
  • 11。什么是构图?创建管道() - bfe.dev
  • 1.实现 curry() - bfe.dev
  • 2.实现带有占位符支持的 curry() - bfe.dev
  • 柯里化 - wikipedia.org
  • 14。实现通用记忆功能 - memo() - bfe.dev
  • 122。实现 memoizeone() - bfe.dev
  • 记忆 - wikipedia.org
  • 部分应用 - wikipedia.org
  • 139。实现 _.partial() - bfe.dev

相关专题

更多
js获取数组长度的方法
js获取数组长度的方法

在js中,可以利用array对象的length属性来获取数组长度,该属性可设置或返回数组中元素的数目,只需要使用“array.length”语句即可返回表示数组对象的元素个数的数值,也就是长度值。php中文网还提供JavaScript数组的相关下载、相关课程等内容,供大家免费下载使用。

536

2023.06.20

js刷新当前页面
js刷新当前页面

js刷新当前页面的方法:1、reload方法,该方法强迫浏览器刷新当前页面,语法为“location.reload([bForceGet]) ”;2、replace方法,该方法通过指定URL替换当前缓存在历史里(客户端)的项目,因此当使用replace方法之后,不能通过“前进”和“后退”来访问已经被替换的URL,语法为“location.replace(URL) ”。php中文网为大家带来了js刷新当前页面的相关知识、以及相关文章等内容

372

2023.07.04

js四舍五入
js四舍五入

js四舍五入的方法:1、tofixed方法,可把 Number 四舍五入为指定小数位数的数字;2、round() 方法,可把一个数字舍入为最接近的整数。php中文网为大家带来了js四舍五入的相关知识、以及相关文章等内容

706

2023.07.04

js删除节点的方法
js删除节点的方法

js删除节点的方法有:1、removeChild()方法,用于从父节点中移除指定的子节点,它需要两个参数,第一个参数是要删除的子节点,第二个参数是父节点;2、parentNode.removeChild()方法,可以直接通过父节点调用来删除子节点;3、remove()方法,可以直接删除节点,而无需指定父节点;4、innerHTML属性,用于删除节点的内容。

470

2023.09.01

JavaScript转义字符
JavaScript转义字符

JavaScript中的转义字符是反斜杠和引号,可以在字符串中表示特殊字符或改变字符的含义。本专题为大家提供转义字符相关的文章、下载、课程内容,供大家免费下载体验。

388

2023.09.04

js生成随机数的方法
js生成随机数的方法

js生成随机数的方法有:1、使用random函数生成0-1之间的随机数;2、使用random函数和特定范围来生成随机整数;3、使用random函数和round函数生成0-99之间的随机整数;4、使用random函数和其他函数生成更复杂的随机数;5、使用random函数和其他函数生成范围内的随机小数;6、使用random函数和其他函数生成范围内的随机整数或小数。

989

2023.09.04

如何启用JavaScript
如何启用JavaScript

JavaScript启用方法有内联脚本、内部脚本、外部脚本和异步加载。详细介绍:1、内联脚本是将JavaScript代码直接嵌入到HTML标签中;2、内部脚本是将JavaScript代码放置在HTML文件的`<script>`标签中;3、外部脚本是将JavaScript代码放置在一个独立的文件;4、外部脚本是将JavaScript代码放置在一个独立的文件。

652

2023.09.12

Js中Symbol类详解
Js中Symbol类详解

javascript中的Symbol数据类型是一种基本数据类型,用于表示独一无二的值。Symbol的特点:1、独一无二,每个Symbol值都是唯一的,不会与其他任何值相等;2、不可变性,Symbol值一旦创建,就不能修改或者重新赋值;3、隐藏性,Symbol值不会被隐式转换为其他类型;4、无法枚举,Symbol值作为对象的属性名时,默认是不可枚举的。

535

2023.09.20

苹果官网入口直接访问
苹果官网入口直接访问

苹果官网直接访问入口是https://www.apple.com/cn/,该页面具备0.8秒首屏渲染、HTTP/3与Brotli加速、WebP+AVIF双格式图片、免登录浏览全参数等特性。本专题为大家提供相关的文章、下载、课程内容,供大家免费下载体验。

10

2025.12.24

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
Git 教程
Git 教程

共21课时 | 2.2万人学习

Git版本控制工具
Git版本控制工具

共8课时 | 1.5万人学习

Git中文开发手册
Git中文开发手册

共0课时 | 0人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号