Javascript - 无法实例化同一对象的多个实例 [英] Javascript - Cannot instantiate multiple instances of the same object
本文介绍了Javascript - 无法实例化同一对象的多个实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试实例化同一对象的多个实例。第一个实例化工作正常,但是当我尝试初始化另一个对象时,我收到此错误,
I am trying to instantiate multiple instances of the same object. The first instantiation works fine, but when I try to initialize another object, I get this error,
未捕获TypeError:Object#< draw>没有方法'width'
这里是小提琴,这是我的代码:
here is the fiddle, and here is my code:
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);
总之,我的目标是实例化同一类的多个实例。
In summary, my objective is to instantiate multiple instances of the same "class".
推荐答案
您正在重新定义 halo
cunstructor:
You are redefining halo
cunstructor:
var halo = new halo(); // <-- change variable name to halo1
halo.width(500);
var halo2 = new halo();
halo2.width(300);
修正版: http://jsfiddle.net/GB4JM/1/
这篇关于Javascript - 无法实例化同一对象的多个实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文