【问题标题】:What is the use of the following code. What is it supposed to do下面的代码有什么用。它应该做什么
【发布时间】:2019-08-01 20:11:44
【问题描述】:

以下代码的用途是什么以及预期的行为是什么。

以非常复杂的方式使用绑定调用和应用

var test = Function.prototype.call.bind(Function.prototype.bind,Function.prototype.call)
var mapGet = test(Function.prototype.apply);
var arrayIndex = test(Array.prototype.slice);

【问题讨论】:

标签: javascript call apply bind


【解决方案1】:

此代码可能已被作者故意混淆。

arrayIndex 的工作方式与Array.prototype.slice 相同,语法如下:

arrayIndex(arr, startIndex, [endIndex])

mapGet是一个函数,可以如下调用:

mapGet(fn, context, arr)

它返回调用fn的结果,并使用给定的context在数组arr中传递参数

var test = Function.prototype.call.bind(Function.prototype.bind,Function.prototype.call)
var mapGet = test(Function.prototype.apply);
var arrayIndex = test(Array.prototype.slice);

console.log(
  arrayIndex([1, 2, 3, 4], 1, 3) // [2, 3]
);

console.log(
  mapGet((x, y) => x + y, null, [10, 20]) //  30
);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-24
    • 1970-01-01
    相关资源
    最近更新 更多