我想實例化同一對象的多個實例。第一個實例工作正常,但是當我嘗試初始化另一個對象,我得到這個錯誤,Javascript - 不能實例化同一對象的多個實例
Uncaught TypeError: Object #<draw> has no method 'width'
這裏是fiddle,這裏是我的代碼:
function halo() {
var width = 720, // default width
height = 80; // default height
function draw() {
// main code
console.log("MAIN");
}
draw.width = function(value) {
if (!arguments.length) return width;
width = value;
return draw;
};
draw.height = function(value) {
if (!arguments.length) return height;
height = value;
return draw;
};
return draw;
}
var halo = new halo();
halo.width(500);
var halo2 = new halo();
halo2.width(300);
綜上所述,我的目標是實例化同一個「類」的多個實例。
難道不應該被'返回新'暈'的底部繪製'? – FakeRainBrigand 2013-03-22 05:51:11
不是,在這種情況下,這不是我們所需要的。 'return draw'就好像它是'return {width:function(){},height:function(){}}'一樣。 – dfsq 2013-03-22 06:00:21
啊,對不起。你是對的。我在想原型。 – FakeRainBrigand 2013-03-22 06:01:14