我想找到定义JavaScript对象(尽可能多的对象)的简单方法。
prototype.member
语法来避免副作用吗?代码示例:
function Hello() {
this.name = 'hello';
}
Hello.prototype = {
hello: function() {
return 'hello';
}
}
是的你可以。唯一的副作用是您丢失了原始原型对象的Constructor属性。您甚至可能不使用它,但是如果这样做,解决方案很简单:自己添加构造函数属性:
function Hello() {
this.name = 'hello';
}
Hello.prototype = {
hello: function() {
return 'hello';
},
constructor: Hello
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句