Function.apply在super上的bug解决方案A
作者:drmingdrmer 类型:原创 来源:闪吧
Function.apply在继承的类上使用会出现问题,参见:http://www.flash8.net/bbs/dispbbs.asp?BoardID=38&ID=238991
因为要实现不定个参数的调用,所以这个东西都用pcode写的,用flasm编译,没有as的源代码可以看- -..使用的时候需要直接在flash里添加这段代码:
这是flash 6中就出现的,为了保持最大兼容性,flash7也继承了这个bug。= =。。
mx.utils.Delegate很有用的1个dd,也使用了apply来实现的,由于很多时候只是想delegate1个类的方法,所以就改用方法名来做delegate,而不是函数的引用。改写后的delegate不在使用apply调用函数,可以避免这个bug。
新写的delegate的调用接口:
__xpLib__.createMethodDelegate(obj:Object,method:String):Function
其中obj是对象应用,method是这个对象上的1个方法的名字。 和mx.utils.Delegate 1样,它返回1个代理函数,调用这个函数就可以自动将它的this指针指向obj参数的对象上。
例子:
var q=__xpLib__.createMethodDelegate(_root,"tt");
function tt(a,b){
trace(a+" "+b);
}
q(12,14);
//output:
12 14
function tt(a,b){
trace(a+" "+b);
}
q(12,14);
//output:
12 14
因为要实现不定个参数的调用,所以这个东西都用pcode写的,用flasm编译,没有as的源代码可以看- -..使用的时候需要直接在flash里添加这段代码:
#initclip
__bytecode__("88140002005F676C6F62616C005F5F78704C69625F5F0096020008001C96020008014E9D0200FA0096020008001C96020008019B050000000000004F9609000070726F746F0008001C96020008014E960B000070726F746F74797065004E3C96020008001C96020008014E961600006372656174654D6574686F6444656C6567617465009B10000002006F626A006D6574686F64008A008E08000000000B26005700960A000401006C656E677468004E8701000A4C129D02001000514C96020004014D4E4D4C9D0200F0FF17960200040A960A0004010063616C6C6565004E4C96080000746172676574004E4D9606000066756E63004E523E4C4C960D000074617267657400006F626A001C4F960E000066756E6300006D6574686F64001C4F3E4F00");
#endinitclip
pcode的代码:
#initclip
if (!_global.__xpLib__)
{
_global.__xpLib__ = function ()
{
};
var proto = _global.__xpLib__.prototype;
_global.__xpLib__.createMethodDelegate = function (obj, method)
{
"function2 () (r:1=’arguments’)";
"push r:arguments, ’length’";
"getMember";
"setRegister r:10";
"dup";
"not";
"branchIfTrue out";
"loop:";
"decrement";
"dup";
"push r:arguments";
"swap";
"getMember";
"swap";
"dup";
"branchIfTrue loop";
"out:";
"pop";
"push r:10";
"push r:arguments, ’callee’";
"getMember";
"dup";
"push ’target’";
"getMember";
"swap";
"push ’func’";
"getMember";
"callMethod";
"return";
"end";
"dup"
"dup"
"push ’target’, obj"
"getVariable"
"setMember"
"push ’func’, method"
"getVariable"
"setMember"
"return"
};
}
#endinitclip
__bytecode__("88140002005F676C6F62616C005F5F78704C69625F5F0096020008001C96020008014E9D0200FA0096020008001C96020008019B050000000000004F9609000070726F746F0008001C96020008014E960B000070726F746F74797065004E3C96020008001C96020008014E961600006372656174654D6574686F6444656C6567617465009B10000002006F626A006D6574686F64008A008E08000000000B26005700960A000401006C656E677468004E8701000A4C129D02001000514C96020004014D4E4D4C9D0200F0FF17960200040A960A0004010063616C6C6565004E4C96080000746172676574004E4D9606000066756E63004E523E4C4C960D000074617267657400006F626A001C4F960E000066756E6300006D6574686F64001C4F3E4F00");
#endinitclip
pcode的代码:
#initclip
if (!_global.__xpLib__)
{
_global.__xpLib__ = function ()
{
};
var proto = _global.__xpLib__.prototype;
_global.__xpLib__.createMethodDelegate = function (obj, method)
{
"function2 () (r:1=’arguments’)";
"push r:arguments, ’length’";
"getMember";
"setRegister r:10";
"dup";
"not";
"branchIfTrue out";
"loop:";
"decrement";
"dup";
"push r:arguments";
"swap";
"getMember";
"swap";
"dup";
"branchIfTrue loop";
"out:";
"pop";
"push r:10";
"push r:arguments, ’callee’";
"getMember";
"dup";
"push ’target’";
"getMember";
"swap";
"push ’func’";
"getMember";
"callMethod";
"return";
"end";
"dup"
"dup"
"push ’target’, obj"
"getVariable"
"setMember"
"push ’func’, method"
"getVariable"
"setMember"
"return"
};
}
#endinitclip
责任编辑:uufeng 时间:2005年7月11日
- 最近更新
