有这样一个类,AAA = cc.Scene.extend({xxxx})
有这样一个方法,
function(a,b,c…){
new AAA(a,b,c,…)
}
请问,如何在new AAA时动态传入若干个参数。。
这些a,b,c…参数可以通过arguments得到,但是怎么传进去呢
这个new方法感觉也没法通过 AAA.prototype.apply方式调用呀。。
某QQ群的大神Fish.Haotian给了解决方案,测试成功
function AAA(a,b){
cc.log("AAA's arguments",arguments);
this.a = a;
this.b = b;
}
function ff(){
cc.log("ff()",arguments);
function construct(constructor, args) {
function F() {
return constructor.apply(this, args);
}
F.prototype = constructor.prototype;
return new F();
}
var aa = construct(AAA,arguments);
cc.log("aa.a=",aa.a);
cc.log("aa.b=",aa.b);
}
ff(100,200);
ff("a","b","c","d");
打印结果:
ff()
AAA's arguments
aa.a= 100
aa.b= 200
ff() "a", "b", "c", "d"]
AAA's arguments "a", "b", "c", "d"]
aa.a= a
aa.b= b
引擎内部在new的时候会回掉ctor方法
cc.Scene.extend({
ctor: function(a, b){
cc.Scene.prototype.ctor.call(this);
//
}
});
其实是cc.Class这个基础类中调用了ctor:
cc.Class = function(){
if(this.ctor)
this.ctor.apply(this, arguments);
};