a是一个函数,那么`a.call.call`真的有用吗? [英] a is a function, then what `a.call.call` really do?

查看:175
本文介绍了a是一个函数,那么`a.call.call`真的有用吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这些代码在chrome devtool上运行。

These codes are run on chrome devtool.

似乎 b.call (与 a.call.call 相同)正在调用第一个参数,是一个函数,然后将第二个参数传递为 this
如果第一个参数不是函数,则抛出而不是函数错误。

It seems like b.call(same as a.call.call) is calling the first argument, which is a function, then pass the second argument as this. If the first argument is not a function, then throw not a function error.

可以某人解释如何< Function> .call.call 工作?

Can someone explain how <Function>.call.call work?

推荐答案

让我举个例子。

function a() { console.log(1) }

function b() { console.log(2) }

a.call(b)    // 1

a.call.call(b)    // 2

a.call.call.call(b)    // 2

为什么?

我们知道 a.call(b)表示调用 a() b

We know a.call(b) means invoke a() with this value b.

这样 a.call.call(b)表示使用值调用 Function.prototype.call() b,与相同。Function.prototype.call.call(b)

So that a.call.call(b) means invoke Function.prototype.call() with this value b, same as Function.prototype.call.call(b).

但是 Function.prototype.call.call()不是普通的函数Object。它可以被调用,但它没有属性。有一些唯一规则来调用它。

But Function.prototype.call.call() is not an ordinary function Object. It can be invoked but it has no property. There's some unique rules to invoke it.

Function.prototype.call.call(a)    // 1

Function.prototype.call.call(b)    // 2

实际上, Function.prototype.call.call(b)是一个异域对象,还有一个绑定函数异类对象。

In fact, Function.prototype.call.call(b) is an Exotic Object, furthermore, a Bound Function Exotic Object.


  • [规范]绑定函数是包装另一个函数对象的异域对象。

  • [Specification] A bound function is an exotic object that wraps another function object.

[规范]调用绑定函数通常会调用其包装函数。

[Specification] Calling a bound function generally results in a call of its wrapped function.

这样 Function.prototype.call.call(a)只是意味着 a()

a.call.call(x)表示调用x,即 x()


  • [规格]在 Function.prototype.call(thisArg)中,如果thisArg未定义或者为null,它将被全局对象替换。

  • [Specification] In Function.prototype.call(thisArg), if thisArg is undefined or null, it will be replaced by global object.

a.call.call() mea ns a.call.call(未定义)表示 a.call.call(窗口)表示调用窗口

a.call.call() means a.call.call(undefined) means a.call.call(window) means invoke window.

尝试调用窗口你会得到未捕获TypeError:窗口不是函数,所以尝试调用 a.call.call()你将获得未捕获的TypeError:a.call.call不是函数

Try to invoke window you'll get Uncaught TypeError: window is not a function, so try to invoke a.call.call() you'll get Uncaught TypeError: a.call.call is not a function.

希望它有所帮助。

这篇关于a是一个函数,那么`a.call.call`真的有用吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆