V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐关注
Meteor
JSLint - a JavaScript code quality tool
jsFiddle
D3.js
WebStorm
推荐书目
JavaScript 权威指南第 5 版
Closure: The Definitive Guide
YidaZh
V2EX  ›  JavaScript

Function.call.call(Number, undefined, index) 谁帮忙翻译下,不是很懂

  •  
  •   YidaZh · 2017-09-29 15:09:26 +08:00 · 1893 次点击
    这是一个创建于 2372 天前的主题,其中的信息可能已经有所发展或是发生改变。

    看到生成 0 …(N-1)数列的一种写法

    Array.apply(null, {length: N}).map(Function.call, Number);
    

    map的过程中, 怎么就变成Number(index)了呢?

    4 条回复    2017-09-29 19:08:14 +08:00
    mosliu
        1
    mosliu  
       2017-09-29 15:54:11 +08:00
    改一下,看看能不能明白吧:、

    Array.apply(null, {length: 3}).map(Function.call, (index,obj)=>{return index});
    Maic
        2
    Maic  
       2017-09-29 19:00:08 +08:00 via iPhone
    这样写是为了秀么,好贱啊…… new Array(3).fill(‘ sb ’).map((v,i)=>i)不行么,或者直接 Object.keys(new Array(3))
    Maic
        3
    Maic  
       2017-09-29 19:01:48 +08:00 via iPhone
    Array.apply 就是 newArray 的另一种写法,后面的 function call 就是 map 的回调函数而已
    MinonHeart
        4
    MinonHeart  
       2017-09-29 19:08:14 +08:00
    Array.apply(null, {length: 3}).map(Function.call.bind(Number));

    var callBindNumber = Function.call.bind(Number);
    callBindNumber(value, index, array); 相当于调用 Number.call(value, index, array);
    因为 call 已经 bind 了 Number,第一参数无效,就变成 Number(index, array),Number 忽然第二参数就是 Number(index)
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1385 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 23:43 · PVG 07:43 · LAX 16:43 · JFK 19:43
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.