var Class = function () {
var klass = function () {
};
klass.prototype.init = function () {};
// 定义 prototype 的别名
klass.fn = klass.prototype;
// 定义类的别名
klass.fn.parent = klass;
// 给类添加属性
klass.extend = function (obj) {
var extended = obj.extended;
for (var i in obj) {
klass[i] = obj[i];
}
if (extended) extended(klass)
};
// 给实例添加属性
klass.include = function (obj) {
var included = obj.included;
for (var i in obj) {
klass.fn[i] = obj[i];
}
if (included) included(klass)
};
return klass;
};
var Person = new Class;
Person.include({
save: function(id) { console.log("id is: " + id); },
exists: function(id) { /* ... */ }
});
var person = new Person;
person.save();
关于两个new的问题,第一个是new的是klass的实例,第二个new的是什么?打印出来的Person的原型是klass,person也是klass?这是什么情况?还是我完全理解错了?
鸿蒙传说
相关分类