JavaScript:函数是否应该能够使用Object.create()创建自己的实例
JavaScript: Should a function be able to create instances of itself with Object.create()
我的用例如下:我想创建一个生产各种数据传输对象(DTO)的工厂。它们必须易于序列化,并且必须有一些额外的方法。
我当前的实现方式如下(简化):
window.Dto = function(type, properties)
{
var
self = this,
values = {},
object = Object.create(self);
properties.forEach(function(prop){
Object.defineProperty(object, prop, {
get: function() { return values[prop]; },
set: function(value) { values[prop] = value; },
enumerable: true
});
});
this.getType = function()
{
return type;
};
this.doSomeMagic = function()
{
// ...
};
return object;
};
// creating a DTO of the Transport.Motorized.Car class
var carObject = new Dto("Transport.Motorized.Car", ["vendor", "model", "color", "vmax", "price"]);
(注意:我不想为这些对象中的每一个创建显式类,因为它们有很多,并且是从服务器端导出的。此外,您在上面看到的properties
参数实际上是一个带有验证约束等的元数据映射。)
我用一个循环做了一个快速的性能检查,其中创建了50000个这样的对象。performance.now()
告诉我,它花了1秒多一点——这看起来不错,但并不太令人印象深刻。
我的问题主要是:工厂从自己的原型创建一个实例(如果我正确理解代码的作用)并返回它可以吗?它会有什么副作用?有更好的方法吗?
就我对工厂函数的理解而言,它们的全部意义在于不需要创建函数本身的新实例。相反,它只是返回一个新创建的对象。
因此,我不使用新创建的实例的实例属性(通过this
)(通过new
运算符),而是只创建一个对象(让我们称之为factoryProto
),并将所有"实例"方法分配给该对象。
然后,您可以使用factoryProto
作为新object
:的[[Prototype]]
window.Dto = function(type, properties) {
var factoryProto = {
getType: function() {
return type;
},
doSomeMagic: function() {
// ...
}
},
values = {},
object = Object.create(factoryProto);
properties.forEach(function(prop) {
Object.defineProperty(object, prop, {
get: function() { return values[prop]; },
set: function(value) { values[prop] = value; },
enumerable: true
});
});
return object;
};
// creating a DTO of the Transport.Motorized.Car class
var carObject = Dto("Transport.Motorized.Car", ["vendor", "model", "color", "vmax", "price"]);
如果你想从原型链中完全获利,你可以在工厂函数之外定义factoryProto
。为了跟踪type
,可以将其添加为不可枚举的object
属性:
window.Dto = (function() {
var factoryProto = {
getType: function() {
return this.type;
},
doSomeMagic: function() {
// ...
}
};
return function(type, properties) {
var values = {},
object = Object.create(factoryProto);
properties.forEach(function(prop) {
Object.defineProperty(object, prop, {
get: function() { return values[prop]; },
set: function(value) { values[prop] = value; },
enumerable: true
});
});
Object.defineProperty(object, 'type', {
value: type,
enumerable: false
});
return object;
};
})();
// creating a DTO of the Transport.Motorized.Car class
var carObject = Dto("Transport.Motorized.Car", ["vendor", "model", "color", "vmax", "price"]);
相关文章:
- 在使用object.create创建的对象中使用super
- 正在使用object.create()创建XMLHttpRequest对象
- Apache poi XSSF创建Excel文件-Create返回格式或文件扩展名无效的空文件
- 正在对通过object.create创建的对象设置原型
- 在不使用object.Create的情况下创建具有null原型的javascript对象
- 使用Object.create()创建对象的模式
- Ember模型create()没有创建ID,适配器必须实现“;createRecord”;
- 使用“new”关键字创建的对象和使用“Object.create”创建的对象给出不同的结果
- 如何使用 Object.create() 而不是 new 创建具有私有成员的对象
- Object.create() 未完全创建;.
- Chrome扩展程序:将数据发送到使用chrome.windows.create创建的窗口
- 设置 使用 Object.create() 创建的 JavaScript 对象的属性
- 如何判断对象是使用“Object.create”还是使用文字语法/“new”关键字创建的
- 如何使用object.create创建对象
- 是否可以使用 Object.create 模式来创建 CustomEvent 对象
- 使用 Object.create() 和 Object.assign() 创建对象有什么区别?
- 对于使用Object.create()创建的数组,Array.isArray()返回false
- 使用Object.create创建的Javascript数组-不是真正的数组
- 绑定到由collection.create()创建的模型的错误事件
- 修改使用Object.create()创建的对象原型