我一直在用下面的结构创建javascript类对象。有没有更好的方法来实现这一目标?javascript:创建类的最佳方法
function MyClass(config)
{
this.init(config);
}
MyClass.prototype =
{
that:this,
config:null,
init:function(config)
{
this.config = config;
this.blah();
},
blah:function()
{
if(this.config.blah)
{
console.log(this.config.blah)
}
}
}
new MyClass({blah:"helloWorld"});
“更好” 永远是值得商榷的。我确实认为将'that'和'config'直接放在原型上而不仅仅是实例本身是工作的。您可以在支持它的浏览器中使用['class'语法](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes)。真的,这是最适合你的。 –