我知道您可以使用此函数设置新对象的原型(阅读 mozzilla 文档),但如果它在这样的对象文字中使用,它是否也会创建自己的属性
return Object.create(this);
我也从一个 Klass 文字中知道这个方法,它只复制实例方法
var subclass = function() { };
subclass.prototype = parent.prototype;
klass.prototype = new subclass;
主要是我对 object.create 方法感兴趣
编辑
var Klass = {
init: function(){},
prototype: {
init: function(){}
},
create: function(){
var object = Object.create(this);
console.log('object with class create');
console.log(object);
console.log("object's parent is this");
console.log(this);
object.parent = this;
object.init.apply(object, arguments);
console.log('returned object from create');
console.log(object);
return object;
},
inst: function(){
var instance = Object.create(this.prototype);
console.log('de instance na object create');
console.log(instance);
instance.parent = this;
instance.init.apply(instance, arguments);
console.log('arguments in inst');
console.log(arguments);
return instance;
},
proxy: function(func){
var thisObject = this;
return(function(){
return func.apply(thisObject, arguments);
});
},
include: function(obj){
var included = obj.included || obj.setup;
for(var i in obj)
this.fn[i] = obj[i];
if (included) included(this);
},
extend: function(obj){
var extended = obj.extended || obj.setup;
for(var i in obj)
this[i] = obj[i];
if (extended) extended(this);
}
};
Klass.fn = Klass.prototype;
Klass.fn.proxy = Klass.proxy;
谢谢,理查德