Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/dart/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用apply或call创建无点函数?_Javascript_Functional Programming_Apply_Method Call_Pointfree - Fatal编程技术网

Javascript 使用apply或call创建无点函数?

Javascript 使用apply或call创建无点函数?,javascript,functional-programming,apply,method-call,pointfree,Javascript,Functional Programming,Apply,Method Call,Pointfree,假设我想创建一个修剪输入的函数,一种简单的方法就是 x => x.trim() 另一种方法是 x => String.prototype.trim.call(x) 这样做的好处是,如果x为.trim()定义了覆盖,我仍然可以从原型中获取实现。进一步说,如果我想在.map()中使用相同的东西,我可以 [" foo", "bar "].map(x => x.trim()) [" foo", "bar "].map(x => String.prototype.trim.ca

假设我想创建一个修剪输入的函数,一种简单的方法就是

x => x.trim()
另一种方法是

x => String.prototype.trim.call(x)
这样做的好处是,如果
x
.trim()
定义了覆盖,我仍然可以从原型中获取实现。进一步说,如果我想在
.map()
中使用相同的东西,我可以

[" foo", "bar "].map(x => x.trim())
[" foo", "bar "].map(x => String.prototype.trim.call(x));
我真想做个决定。我不明白的是为什么不能使用用

我也试过了,因为没有争论,实际上是一样的

同样,在地图之外,也不能使用函数
。call
函数

> fn = String.prototype.trim.call
[Function: call]
> fn('asdf')
TypeError: fn is not a function
所以我的问题是

  • 在上面的例子中,
    String.prototype.trim.call返回的函数具体做什么?那是什么功能
  • 为什么我尝试使用
    .call
    时不起作用
  • 使用
    .bind
    .apply
    .call
    是否仍然可以执行此操作

  • 有趣的问题!它归结为以下签名:

    理解您注意到的行为的关键是可选的
    thisArg
    ——它是将提供给回调的上下文

    此外:

    如果为映射提供了thisArg参数,则它将用作回调函数的this值。否则,未定义的值将用作其此值

    这意味着
    undefined
    将用作回调的上下文,在本例中是
    String.prototype.trim.call

    这里是有趣的地方。。。
    Function.prototype.call
    (它继承了
    String.prototype.trim.call)的正常上下文是它调用的函数,即在本例中为
    String.prototype.trim
    Function.prototype.call的第一个参数提供将被调用的函数的上下文

    .map(String.prototype.trim.call)
    的情况下,提供给
    String.prototype.trim.call
    的上下文是
    undefined
    ,这意味着我们试图使用数组中元素的上下文调用
    undefined
    。安全了吗?这肯定有点让人心神不宁

    通过在
    .map(callback,thisArg)
    中提供
    String.prototype.trim
    作为
    thisArg
    并使用
    函数,我们可以利用此行为实现我们想要的效果。call
    作为回调:


    console.log([“foo”,“bar”].map(Function.call,String.prototype.trim))有趣的问题!它归结为以下签名:

    理解您注意到的行为的关键是可选的
    thisArg
    ——它是将提供给回调的上下文

    此外:

    如果为映射提供了thisArg参数,则它将用作回调函数的this值。否则,未定义的值将用作其此值

    这意味着
    undefined
    将用作回调的上下文,在本例中是
    String.prototype.trim.call

    这里是有趣的地方。。。
    Function.prototype.call
    (它继承了
    String.prototype.trim.call)的正常上下文是它调用的函数,即在本例中为
    String.prototype.trim
    Function.prototype.call的第一个参数提供将被调用的函数的上下文

    .map(String.prototype.trim.call)
    的情况下,提供给
    String.prototype.trim.call
    的上下文是
    undefined
    ,这意味着我们试图使用数组中元素的上下文调用
    undefined
    。安全了吗?这肯定有点让人心神不宁

    通过在
    .map(callback,thisArg)
    中提供
    String.prototype.trim
    作为
    thisArg
    并使用
    函数,我们可以利用此行为实现我们想要的效果。call
    作为回调:

    console.log([“foo”,“bar”].map(Function.call,String.prototype.trim))
    我不明白的是为什么不能使用用

    这里的问题是,所有函数都从
    函数继承相同的
    调用
    方法;因此,上述内容相当于

    [" foo", "bar "].map(Function.prototype.call);
    
    要使代码正常工作,
    调用
    需要“记住”它是从
    String.prototype.trim获得的,而不是从其他函数获得的;但这并不是JavaScript中方法调用的工作方式。在像
    foo.bar()
    这样的方法调用表达式中,
    foo
    不仅是其
    bar
    属性作为函数被调用的对象,也是作为
    this
    隐式传递给该函数的对象。在
    String.prototype.trim.call(x)
    的情况下,
    call
    调用
    String.prototype.trim
    的唯一原因是您使用的是方法调用语法,因此它可以从
    这个
    中得到它


  • 在上面的例子中,
    String.prototype.trim.call返回的函数具体做什么?那是什么功能
  • 它是一种方法,它接受一个对象和零个或多个参数,并且在对函数调用时,将该函数作为该对象上的方法调用,并传递这些参数

  • 为什么我尝试使用
    .call
    时不起作用
  • 因为它试图调用
    call
    作为一个自由函数,而不是作为一个方法;因此,
    call
    不会收到一个
    这个
    参数,告诉它调用哪个函数,所以它无法完成它的工作。(它很可能会收到“default object”,例如
    window
    ,作为它的
    this
    参数;但是有一些细节我不确定。您可以尝试
    [“foo”,“bar”].map(函数(){return this;})
    var new_array = arr.map(function callback(currentValue[, index[, array]]) {
        // Return element for new_array
    }[, thisArg])
    
    [" foo", "bar "].map(String.prototype.trim.call);
    
    [" foo", "bar "].map(Function.prototype.call);
    
    [" foo", "bar "].map(Function.prototype.call.bind(String.prototype.trim))
    
    [" foo", "bar "].map(x => x.trim())