Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
168 views
in Technique[技术] by (71.8m points)

javascript - a is a function, then what `a.call.call` really do?

enter image description here

These codes are run on chrome devtool.

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.

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Let me show you an example.

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

Why?

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

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

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

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.

So that Function.prototype.call.call(a) simply means a().

a.call.call(x) means invoke x, that is, x().

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

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

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.

Hope it helps.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.9k users

...