我使用的模式来自: Giulio Canti's blog这样我就可以创建很多构造函数。我希望有一种设置功能名称的方法。也就是说,给定构造函数
function struct(cls, props) {
function Struct(obj) {
// add properties
for (var name in props) {
if (props.hasOwnProperty(name)) {
// here you could implement type checking exploiting props[name]
this[name] = obj[name];
}
}
}
// the following doesn't work for me!
// Struct.name = cls;
return Struct;
}
当我用它来创建一个实例时,例如
var Person = struct('Person', {
name: String,
surname: String
});
var person = new Person({surname: 'Canti', name: 'Giulio'});
在控制台中,我得到以下内容:
但我更愿意:
Person {name: "Giulio", surname: "Canti"}
关于如何实现这一目标的任何建议?