function Card(styleAttr, cardInfo)
{
//Attributes
this.styleAttr = styleAttr;
this.cardInfo = cardInfo;
//Functions
constructCard(this.styleAttr);
}
function constructCard(styleAttr) {
var cardCSS = {
'width':styleAttr.width,
'height':styleAttr.height,
'background-color':'black'
}
$('<div class="Card"></div>').appendTo('body').css(cardCSS);
}
嗨,这个卡类获取其他两个对象的参数。其中之一是styleAttr,它包含一个名为'width'的属性。除非将此对象传递给构造卡,否则我无法访问styleAttr.width属性。上面的例子起作用。但是,如果我这样做:访问方法内的Javascript对象属性
function constructCard() {
var cardCSS = {
'width': this.styleAttr.width, //Undefined
'height':styleAttr.height,
'background-color':'black'
}
$('<div class="Card"></div>').appendTo('body').css(cardCSS);
}
晴代码在其他语言,所以我不知道,我必须给函数constructCard的类绑定到能够访问它的属性还是我被迫通过对象来获取值。或者我应该让它们成为全局变量?
这一定很简单,我没有从Moz Doc中找到。
感谢
谢谢,对你们来说,这更接近于C语法,所以我更喜欢它。 –