调用 exampleFunction() 和 exampleFunction.call() 之间有什么区别? Actionscript 上有一个功能,允许您通过“call”方法使用函数。例如,要使用下面的“test”函数,我可以编写 test() 和 test.call()
function test():void {
trace('function was called')
}
有什么区别?
最佳答案
与 ECMAScript(AS 从中派生)一样,它允许通过第一个参数指定“this”上下文。
请参阅Function.call文档。请注意有关函数与方法的注释 - 或 "bound functions" – 以及示例。
也就是说,给定:
function f ():* { return this; }
var x:Object = {f: f};
然后:
f.call(x)
和 x.f()
的计算结果均为 x
,而 f()
则为不是因为它有一个不同的“this”(它的计算结果是Global
)。
如果使用 f.call()
(无参数),则“this”将为 NaN,而 f()
,如上所述,具有 Global
为“这个”。尝试在测试代码中使用 trace(this)
,因为它将产生更有用的上下文信息。
祝你编码愉快。
关于flash - 调用 example Function() 和 example Function.call() 有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9027829/