我一直在用下面的結構創建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)。真的,這是最適合你的。 –