Javascript 将属性作为参数传递,同时设置同一对象的其他属性
我正在使用Raphael.js制作一个小js游戏。这是我在JS中的第一个大项目,所以我不熟悉所有的设计模式等等。我试图制作一个表示移动计时器的对象。从“litCircle”行开始,我得到错误:Javascript 将属性作为参数传递,同时设置同一对象的其他属性,javascript,raphael,javascript-objects,Javascript,Raphael,Javascript Objects,我正在使用Raphael.js制作一个小js游戏。这是我在JS中的第一个大项目,所以我不熟悉所有的设计模式等等。我试图制作一个表示移动计时器的对象。从“litCircle”行开始,我得到错误: Uncaught TypeError: Cannot read property '0' of undefined c是我的拉斐尔对象。是否有一些我不知道的地方,您不能使用当前在函数调用中定义的对象的属性来帮助设置对象其他属性的值 var moveTimer = { mtScaler : 15,
Uncaught TypeError: Cannot read property '0' of undefined
c是我的拉斐尔对象。是否有一些我不知道的地方,您不能使用当前在函数调用中定义的对象的属性来帮助设置对象其他属性的值
var moveTimer = {
mtScaler : 15,
all : c.set(),
RS : [1*this.mtScaler,3*this.mtScaler,4*this.mtScaler],
litCircle : c.circle(200,37,this.RS[0]),
midCircle : c.circle(200,37,this.RS[1]).attr({
"stroke-dasharray": "- "
}),
bigCircle : c.circle(200,37,this.RS[2]).attr({
"stroke-dasharray":". "}),
orangeOne : c.circle(200, 37,this.RS[0]).attr({
"stroke-width": 2,
"stroke": COLOR_DICT["orange"],
}),
turnCounter : c.text(200,38,0),
orangeAnim :
Raphael.animation({
"50%": { r: this.RS[2] },
"100%": { r: this.RS[0] }
}, 3000),
finish : function(){
this.all.push(this.litCircle, this.midCircle, this.bigCircle,
this.orangeOne, this.turnCounter);
this.all.transform("t250,230");
}
}
在您调用RS时,RS仍在定义中,请在定义之外定义一些变量并利用这些变量 例如:
var scaler, rs;
scaler = 15;
rs = [1*tscaler,3*tscaler,4*scaler];
var moveTimer = {
mtScaler : scaler,
all : c.set(),
RS : rs,
litCircle : c.circle(200,37,rs[0]),
midCircle : c.circle(200,37,rs[1]).attr({
"stroke-dasharray": "- "
}),
bigCircle : c.circle(200,37,this.RS[2]).attr({
"stroke-dasharray":". "}),
orangeOne : c.circle(200, 37,this.RS[0]).attr({
"stroke-width": 2,
"stroke": COLOR_DICT["orange"],
}),
turnCounter : c.text(200,38,0),
orangeAnim :
Raphael.animation({
"50%": { r: this.RS[2] },
"100%": { r: this.RS[0] }
}, 3000),
finish : function(){
this.all.push(this.litCircle, this.midCircle, this.bigCircle,
this.orangeOne, this.turnCounter);
this.all.transform("t250,230");
}
}
这可能不是100%,因为我不能确认代码,但这是一个起点
那
this
atlitCircle:c.circle(200,37,this.RS[0])
表示调用方,Window
对象,而不是您定义的moveTimer
显然,
Window
对象没有属性RS
如果您不喜欢声明全局变量,可以定义一个构造函数来生成movetimer对象。可能类似于:函数generateMoveTimer(){this.mtScaler=15;this.RS=[1*this.mtScaler,2*this.mtScaler,2*this.mtScaler,];…}var moveTimer=new generateMoveTimer();