当前位置: 首页> 财经> 金融 > 软文营销网站_哈尔滨seo排名优化免费咨询_手机怎么在百度上发布信息_北京营销公司比较好的

软文营销网站_哈尔滨seo排名优化免费咨询_手机怎么在百度上发布信息_北京营销公司比较好的

时间:2025/7/11 17:33:53来源:https://blog.csdn.net/Zheng113/article/details/142497226 浏览次数:0次
软文营销网站_哈尔滨seo排名优化免费咨询_手机怎么在百度上发布信息_北京营销公司比较好的

固定某些参数,返回接受剩余参数的新函数,如果没有剩余参数,就调用。
将多个参数的函数转换为单个参数的函数
作用:参数复用,延迟计算…

// 传入参数不限,不能丢失传入的参数
function add() {// 不设置形参 因为传入参数不定// 用args存放传入参数 arguments是函数参数 但是是对象 但是这里想要args是数组 要进行类型转换 // let args = arguments// Array.prototype.slice.call将具有length属性的arguments转换为数组let args = Array.prototype.slice.call(arguments)let inner = function () {// inner接收剩余传入的参数,把当前传入的参数 加入到之前的参数数组里args.push(...arguments)// let sum = args.reduce((pre, val) => pre + val, 0)// return sum// 内部函数返回内部函数 可以处理多个括号return inner}// 函数被隐式转换了inner.toString = function () {let sum = args.reduce((pre, val) => pre + val, 0)return sum}return inner
}
// 两种方法拿到结果
console.log(+add(1)(2)(3))//输出函数被隐式转换成字符串
console.log(add(1)(2)(3, 4).toString())

对Array.prototype.slice.call(arguments)的解释
作用:将具有length属性的arguments转换为数组,可以看做是arguments.toArray().slice(),分成两步理解:

(1)其中,arguments是一个具有length属性的对象, 通过call 这个方法,把arguments 指向了Array.prototype.slice方法的作用域,也就是说通过call方法,让Array.prototype.slice对arguments对象进行操作。
(2)Array.prototype.slice就是对该对象使用Array类的slice方法。但是arguments又不是个Array对象。
因为:typeof arguments === “Object” //不是"Array",用arguments.slice()方法,这样是会报错的。
所以这里,我们需要使用Array.prototype.slice, 它的作用就是第一步把arguments转换为一个Array对象,第二步对转换后的Array对象使用slice方法。


参数个数固定时可以这样写:

// 传入5个参数才进行计算
let nums = []
function sum(...args) {// 把每次当前接受到的数 放入到数组中nums.push(...args)// 累加当前的数值 初始值为0if (nums.length === 5) {const res = nums.reduce((pre, val) => pre + val, 0)// 要清空 不然每次往nums后面加nums = []return res} else {// 内部返回接受剩余参数的新函数return sum}
}
console.log(sum(1)(2)(3)(4)(5));//15
console.log(sum(1, 2)(3)(4)(6));//16// 参数个数可以自定义
function sumMaker(len) {let nums = []function sum(...args) {// 把每次当前接受到的数 放入到数组中nums.push(...args)// 累加当前的数值 初始值为0if (nums.length >= len) {const res = nums.slice(0, len).reduce((pre, val) => pre + val, 0)// 要清空 不然每次往nums后面加nums = []return res} else {// 内部返回接受剩余参数的新函数return sum}}return sum
}
console.log(sumMaker(5)(1, 2)(3)(4)(5, 6))//15
关键字:软文营销网站_哈尔滨seo排名优化免费咨询_手机怎么在百度上发布信息_北京营销公司比较好的

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: