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語法,所以我更喜歡它。 –