Skip to content

Instantly share code, notes, and snippets.

@6174
Created August 3, 2013 11:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 6174/6146100 to your computer and use it in GitHub Desktop.
Save 6174/6146100 to your computer and use it in GitHub Desktop.
Klass.js 多重继承!
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