您当前的位置: 首页 >  Java

暂无认证

  • 0浏览

    0关注

    92582博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

JavaScript实现累加/累乘、构造函数、arguments

发布时间:2022-02-11 11:00:45 ,浏览量:0

目录
  • 1、代码实现
  • 2、执行代码
1、代码实现
function Compute() { this.plus = function () { return loop(arguments, 'add', 0); } this.times = function () { return loop(arguments, 'mul', 1); } function loop(args, method, result) { for (let i = 0; i < args.length; i++) { const item = args[i]; if (method == 'add') { result += item; } else if (method == 'mul') { result *= item; } } return result; } } 
2、执行代码
// 实例化Compute构造函数 let compute = new Compute(); console.log(compute.plus(1, 2, 3)); // 6 console.log(compute.times(2, 3, 2)); // 12 
关注
打赏
1653961664
查看更多评论
立即登录/注册

微信扫码登录

0.7764s