Skip to content

Instantly share code, notes, and snippets.

@black-black-cat
Last active July 5, 2016 13:01
Show Gist options
  • Save black-black-cat/718673b89d1daa3fcde28244b4c9bae0 to your computer and use it in GitHub Desktop.
Save black-black-cat/718673b89d1daa3fcde28244b4c9bae0 to your computer and use it in GitHub Desktop.
create a class
var createClass = function (Parent, props) {
if (arguments.length === 1) {
props = Parent;
Parent = null;
}
var Child, F, i;
// 1. 构造函数
Child = function Klass() {
if (Child.uber && Child.uber.hasOwnProperty("__construct")) {
Child.uber.__construct.apply(this, arguments);
}
if (Child.prototype.hasOwnProperty("__construct")) {
Child.prototype.__construct.apply(this, arguments);
}
};
// 2. 继承
Parent = Parent || Object;
F = function () {};
F.prototype = Parent.prototype;
Child.prototype = new F();
Child.uber = Parent.prototype;
Child.prototype.constructor = Child;
// 3. 添加方法实现
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