createSequence :方法的执行顺序。
createSequence : function(fcn, scope){ if(typeof fcn != "function"){ return this; } var method = this; return function() { var retval = method.apply(this || window, arguments); fcn.apply(scope || this || window, arguments); return retval; }; }
Function.prototype.sequence=function(fcn, scope){ if(typeof fcn != "function"){ return this; //这个this,就是调用sequence的方法,是方法本身,不是方法的实例 } var method = this; return function() { var retval = method.apply(this || window, arguments); fcn.apply(scope || this || window, arguments); return retval; }; } var cl = function () { name = "hongda"; alert(name); } var cl2 = function () { name = "hongda2"; alert(name); } var cl3 = function () { name = "hongda3"; alert(name); } var fun = cl.sequence(cl3).sequence(cl2); fun(); //hongda hongda3 hongda2
有参数
var cl = function (a) { name = "hongda"; alert(name+"||"+a); } var cl2 = function (a) { name = "hongda2"; alert(name + "||" + a); } var cl3 = function (a) { name = "hongda3"; alert(name + "||" + a); } var fun = cl.createCallback("aaaaaa").sequence(cl3.createCallback("cccccc")).sequence(cl2.createCallback("bbbb"));// var fun = function () { cl("aa"); } .sequence(function () { cl3("cc"); }).sequence(function () { cl2("bb"); }); // 一样有效, 结构不太好 fun(); //fcn为方法,不是方法的执行,不能用cl("aa").sequence(cl2("bb"))... 这样调用
看method(this)
Function.prototype.sequence = function (fcn, scope) { console.log("类"); console.dir(this); if (typeof fcn != "function") { return this; } var method = this; return function () { var args = Array.apply(null, arguments); console.log("arguments:" + args); var retval = method.apply(this || window, arguments); //retval为undefined,下次调用时它就是(method),下次时该方法无效 fcn.apply(scope || this || window, arguments); //这两个arguments没起到作用,因为都是将方法本身传进来的,方法名没有参数 return retval; }; } var cl = function () { this.name = "hongda"; alert(this.name); } cl.school = "cl"; var cl2 = function () { this.name = "hongda2"; alert(this.name); } cl2.school = "cl2"; var cl3 = function () { this.name = "hongda3"; alert(this.name); } cl3.school = "cl3"; var fun = cl.sequence("d");
extend
extend : function(){ // inline overrides var io = function(o){ for(var m in o){ this[m] = o[m]; } }; var oc = Object.prototype.constructor; return function(sb, sp, overrides){ if(typeof sp == 'object'){ overrides = sp; sp = sb; sb = overrides.constructor != oc ? overrides.constructor : function(){sp.apply(this, arguments);}; } var F = function(){}, sbp, spp = sp.prototype; F.prototype = spp; sbp = sb.prototype = new F(); sbp.constructor=sb; sb.superclass=spp; if(spp.constructor == oc){ spp.constructor=sp; } sb.override = function(o){ Ext.override(sb, o); }; sbp.override = io; Ext.override(sb, overrides); sb.extend = function(o){Ext.extend(sb, o);}; return sb; }; }(),
function extend() { var io = function (o) { for (var m in o) { this[m] = o[m]; } }; var oc = Object.prototype.constructor; return function (sb, sp, overrides) { if (typeof sp == 'object') { overrides = sp; sp = sb; sb = overrides.constructor != oc ? overrides.constructor : function () { sp.apply(this, arguments); }; } var F = function () { }, sbp, spp = sp.prototype; F.prototype = spp; sbp = sb.prototype = new F(); sbp.constructor = sb; sb.superclass = spp; if (spp.constructor == oc) { spp.constructor = sp; } sb.override = function (o) { Ext.override(sb, o); }; sbp.override = io; Ext.override(sb, overrides); sb.extend = function (o) { Ext.extend(sb, o); }; return sb; }; } var father = function () { this.name = "hong"; this.age = 23; this.show = function () { alert(this.name + "||" + this.age); } } father.prototype.address = "jiangshu"; father.prototype.alert = function () { alert("father"); } var child = function () { this.school = "nanjing"; } child.prototype.country = "china"; child.prototype.say = function () { alert(this.country); } extend()(child, father, { center: "center", email: "hongda@qq.com" }); var c = new child(); var f = new father(); console.dir(child); console.dir(c); console.dir(father); console.dir(f);
得出:father没有变化,没有继承overrides
child继承了override,继承了father相对应的prototype对象,但没有继承father内部的属性,方法。
child原来的prototype对象的成员被删掉了。