Skip to content

Instantly share code, notes, and snippets.

@roman01la
Created October 26, 2013 13:26
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 roman01la/7169458 to your computer and use it in GitHub Desktop.
Save roman01la/7169458 to your computer and use it in GitHub Desktop.
(function() {
window.Class = function() {};
Class.extend = function (props, staticProps) {
var mixins = [];
if ({}.toString.apply(arguments[0]) == "[object Array]") {
mixins = arguments[0];
props = arguments[1];
staticProps = arguments[2];
}
function Constructor() {
this.init && this.init.apply(this, arguments);
}
Constructor.prototype = Class.inherit(this.prototype);
Constructor.prototype.constructor = Constructor;
Constructor.extend = Class.extend;
copyWrappedProps(staticProps, Constructor, this);
for (var i = 0; i < mixins.length; i++) {
copyWrappedProps(mixins[i], Constructor.prototype, this.prototype);
}
copyWrappedProps(props, Constructor.prototype, this.prototype);
return Constructor;
};
var fnTest = /xyz/.test(function() {xyz}) ? /\b_super\b/ : /./;
function copyWrappedProps(props, targetPropsObj, parentPropsObj) {
if (!props) return;
for (var name in props) {
if (typeof props[name] == "function"
&& typeof parentPropsObj[name] == "function"
&& fnTest.test(props[name])) {
// скопировать, завернув в обёртку
targetPropsObj[name] = wrap(props[name], parentPropsObj[name]);
} else {
targetPropsObj[name] = props[name];
}
}
}
function wrap(method, parentMethod) {
return function() {
var backup = this._super;
this._super = parentMethod;
try {
return method.apply(this, arguments);
} finally {
this._super = backup;
}
}
}
Class.inherit = Object.create || function(proto) {
function F() {}
F.prototype = proto;
return new F;
};
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment