`

Bound methods注意

    博客分类:
  • Flex
阅读更多
与JAVA的不同,flex里面的Bound methods.
Bound methods 定义如下:
   A bound method, sometimes called a method closure, is simply a method that is extracted from its instance.
和 Function Closure 的不同:
   The key difference, however, between a bound method and a function closure is that the this reference for a bound method remains linked, or bound, to the instance that implements the method.
  In other words, the this reference in a bound method always points to the original object that implemented the method. For function closures, the this reference is generic, which means that it points to whatever object the function is associated with at the time it is invoked。
调用的是bound method 时,那么该method 中的this 引用的是实现该方法的对象,并用该方法能够使用实现该方法的对象内的属性。。这一点和As2.0不同。
 
class ThisTest
{
private var num:Number = 3;
function foo():void // bound method defined
{
trace("foo's this: " + this);
trace("num: " + num);
}
function bar():Function
{
return foo; // bound method returned
}
}
var myTest:ThisTest = new ThisTest();
var myFunc:Function = myTest.bar();
trace(this); // output: [object global]
myFunc();
/* output:
foo's this: [object ThisTest]
output: num: 3 */

此代码中的全局this 和 Bound Function 里的this 引用的对象是不同的。
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics