【flex】this 在函数语句定义法和函数表达式定义法中的不同

 

 this 在函数语句定义法和函数表达式定义法中的不同

使用函数语句定义法,this 关键字牢牢指向当前函数定义的域;而若使用函数表达式定义法,则随着函数附着的对象不同,this关键字也随之改变。

另外一个显著的区别是在内存管理和垃圾回收方面。因为,函数表达式不像对象那样独立存在,它相当于一个匿名函数。当持有这个函数引用的数组或对象脱离作用域或由于其他原因不再可用,那么这匿名函数对象引用计数为零,符合垃圾回收条件,这意味着可能被回收。

var num:int = 3;

function testThisA() {

trace (num);

trace (this.num);

trace (this);

}

var testThisB:Function = function() {

trace (num);

trace (this.num);

trace (this);

}

var obj:Object = {num:300};

obj.testB = testThisB;

obj.testA = testThisA;

 

testThisA();

obj.testA();

testThisA.apply(obj);//用apply 试图将testThisA()的this 关键字指向绑定到obj 上,发现输出没改变

/*以上3 个输出的都是:

3

3

[object MainTimeline]

*/

testThisB();

/*输出:

3

3

[object MainTimeline]

*/

obj.testB();

/*输出:

3

300

[object Object]

*/

testThisB.apply(obj);//用apply 试图将testThisB()的this 关键字指向绑定到obj 上,发现输出改变,绑定成功

/*输出:

3

300

[object Object]

*/

 

[扩展]:Function类

apply

()

方法

call

()

方法

英文解释:

Specifies the value of thisObject to be used within any function that ActionScript calls. This method also specifies the parameters to be passed to any called function. Because apply() is a method of the Function class, it is also a method of every Function object i n ActionScript.

The parameters are specified as an Array object, unlike Function.call(), which specifies parameters as a comma-delimited list. This is often useful when the number of parameters to be passed is not known until the script actually executes.

Returns the value that the called function specifies as the return value.

中文解释:

指定要在 ActionScript 调用的任何函数内使用的 thisObject 的值。此方法还指定要传递给任何被调用函数的参数。由于 apply() 是 Function 类的方法,所以它也是 ActionScript 中每个 Function 对象的方法。

与 Function.call() (它将参数指定为用逗号分隔的列表)不同,该方法将参数指定为一个 Array 对象。如果在脚本实际执行前,无法知道要传递的参数的数量,那么这种方法通常很有用。

返回被调用函数指定为返回值的值。

 

示例

几乎在所有的情形下,都可以使用函数调用运算符 (()) 来代替此方法。函数调用运算符使代码简明易读。如果需要显式控制函数调用中的 thisObject 参数,则此方法很有用。通常,如果在函数体内将函数作为对象的方法来调用,则 thisObject 设置为 myObject,如下例所示:

  myObject.myMethod(1, 2, 3);

 

    在某些情况下,您可能希望 thisObject 指向其他位置,例如,函数必须作为对象的方法进行调用,但该函数实际上并不作为该对象的方法进行存储的情况。

  myObject.myMethod.call(myOtherObject, 1, 2, 3);

 

    可以为 thisObject 参数传递值 null,以便将函数作为一个常规函数而非对象的方法来调用。例如,以下函数调用是等效的:

  Math.sin(Math.PI / 4)

  Math.sin.call(null, Math.PI / 4)

 

代码例子1:

var arr1:Array=[1,3,4];

var arr2=[3,4,5];

arr1.push.apply(arr1,arr2);

trace(arr1.length);  

//打印出:6

【注释】:数组的话,参数是一个一个放(push)进去的。

 

代码例子2:

//打印出数组中的最大值:

var arr:Array = new Array(1,2,3);

trace(Math.max.apply(null,arr));

//打印出:3

 

代码例子3:

var arr:Array = new Array(1,2,3);

var myObj:Object = new Object();

//此函数头也可以写成:“function theFunction(arr:int) {”。注意:数组的话,参数是一个一个传进去的,所以参数不能写成“arr:Array”否则报错!

function theFunction() {

       trace(this);

       trace(arguments);

trace(arguments.length);

}

var theFunction1:Function = function() {

       trace(this);

       trace(arguments);

trace(arguments.length);

}

 

trace(theFunction.apply(null,arr));

/**打印出:

arrytest

1,2,3

3

Undefined

**/

 

trace(theFunction1.apply(null,arr));

/**打印出:

[object global]

1,2,3

3

Undefined

**/

 

trace(theFunction.apply(myObj,arr));

/**打印出:

arrytest

1,2,3

3

Undefined

**/

 

trace(theFunction1.apply(myObj,arr));

/**打印出:

[object Object]

1,2,3

3

Undefined

**/

 

trace(theFunction1.apply(myObj,arr));

/**打印出:

[object Object]

1,2,3

3

Undefined

**/

 

又如如下例子:

public function println(str:String):void {

       trace(  == this.println); // true

       trace(arguments.length);                 // 1

       trace(arguments[0]);                     // Hello World

       trace(str);                                // Hello World

}

 

println("Hello World");

 

说明:arguments:用于存储和访问函数参数的参数对象。注意:传递的参数数目可能与函数声明的数目有所不同。

arguments.callee:对当前正在执行的函数的引用。

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值