【发布时间】:2017-12-29 14:28:42
【问题描述】:
我有这个代码
var a = function(){}
var b = a.call
b() // TypeError: b is not a function
typeof b 是 'function' 和 console.log(b) 显示 ƒ call() { [native code] }。
有人可以解释一下这种行为吗?
【问题讨论】:
-
您为什么要这样做?看起来很奇怪。
-
@Andy:OP 可能认为
a会自动绑定到.call。如果正确完成,这种绑定函数有合法用途。就像您预期收到一个应该是a参数的项目数组一样,但第一个数组项目实际上是它的this值。因此,如果您将a绑定到call并存储它,您就可以像boundA.apply(null, args)一样调用它,或者在现代环境中,boundA(...args)。 -
即使用它的原因是能够为
a切换上下文 -
@epascarello 因为我不想到处写
Object.prototype.hasOwnProperty.call。
标签: javascript function call first-class-functions