Skip to content

Instantly share code, notes, and snippets.

@edom18
Created March 27, 2013 06:03
Show Gist options
  • Save edom18/5252060 to your computer and use it in GitHub Desktop.
Save edom18/5252060 to your computer and use it in GitHub Desktop.
2013-03-27 1st
(function (win, doc, exports, undefined) {
'use strict';
var fnTest, initialize;
fnTest = /xyz/.test(function(){xyz;}) ? /\b_super\b/ : /.*/;
initialize = false;
function Class() { /* noop. */ }
Class.extend = function (props) {
var SuperClass, prototype;
SuperClass = this;
initialize = true;
prototype = new this();
initialize = false;
function Class() {
if (initialize !== true && typeof this.init === 'function') {
this.init.apply(this, arguments);
}
}
for (var key in props) {
var prop = props[key],
_super = SuperClass.prototype[key],
isMethodOverride = (typeof prop === 'function' && typeof _super === 'function' && fnTest.test(prop));
if (isMethodOverride) {
prototype[key] = (function (fn, _super) {
return function () {
var ret,
tmp = this._super;
this._super = _super;
ret = fn.apply(this, arguments);
this._super = tmp;
return ret;
};
}(prop, _super));
}
else {
prototype[key] = prop;
}
}
Class.prototype = prototype;
Class.prototype.constructor = Class;
Class.extend = SuperClass.extend;
return Class;
};
exports.Class = Class;
}(window, window.document, window));
var Parson = Class.extend({
init: function (name) {
this.name = name;
},
say: function () {
console.log(this.name);
},
});
var Child = Parson.extend({
init: function (name) {
this._super(name);
this.age = 4;
},
say: function () {
this._super();
console.log('And I am ' + this.age + ' years old!');
}
});
var Grandson = Child.extend({
init: function (name) {
this._super(name);
this.sex = 'male';
},
say: function () {
this._super();
console.log('I am ' + this.sex + '.');
}
});
debugger;
var p = new Parson('hoge');
var c = new Child('fuga');
var g = new Grandson('bar');
p.say();
c.say();
g.say();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment