我正在尝试实例化同一对象的多个实例。第一次实例化工作正常,但是当我尝试初始化另一个对象时,出现此错误,
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);
总而言之,我的目标是实例化同一“类”的多个实例。
最佳答案
您正在重新定义 halo
cunstructor:
var halo = new halo(); // <-- change variable name to halo1
halo.width(500);
var halo2 = new halo();
halo2.width(300);
关于Javascript - 无法实例化同一对象的多个实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15563435/