Created
August 3, 2013 11:04
-
-
Save 6174/6146100 to your computer and use it in GitHub Desktop.
Klass.js 多重继承!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var xuejia = { | |
/** | |
* proxy | |
*/ | |
proxy: function(context, func) { | |
return function() { | |
func.apply(context, arguments); | |
} | |
}, | |
/** | |
* function wraper | |
*/ | |
wrap: function(func, wrapper) { | |
var __method = func; | |
return function() { | |
var args = Array.prototype.slice.call(arguments); | |
return wrapper.apply(this, [__method.bind(this)].concat(args)); | |
} | |
/* | |
eg: | |
f=wrap(f, function(org,x,y){ | |
console.log("x:"+x,"y:"+y); | |
var r=org(x,y); | |
console.log("result:"+r); | |
}); | |
*/ | |
} | |
}; | |
if (typeof Object.create !== "function") { | |
Object.create = function(o) { | |
function F() {} | |
F.prototype = o; | |
return new F(); | |
}; | |
} | |
/** | |
*Klass 语法糖 | |
*/ | |
xuejia.Klass = function(Parent, props) { | |
var Child, F, i; | |
Child = function() { | |
var parent = Child.parent; | |
while(parent){ | |
parent.prototype && parent.prototype.hasOwnProperty("__construct") && parent.prototype.__construct.apply(this, arguments); | |
parent = parent.parent; | |
} | |
if (Child.prototype.hasOwnProperty("__construct")) { | |
Child.prototype.__construct.apply(this, arguments); | |
} | |
this.super = Parent.prototype; | |
}; | |
Parent = Parent || Object; | |
F = function() {}; | |
F.prototype = Parent.prototype; | |
Child.prototype = new F(); | |
Child.parent = Parent; | |
Child.super = Parent.prototype; | |
Child.prototype.constructor = Child; | |
for (i in props) { | |
if (props.hasOwnProperty(i)) { | |
Child.prototype[i] = props[i]; | |
} | |
} | |
return Child; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment