所以,我相對比較新的JavaScript,並仍然有困難讓我的頭一輪變量範圍。所以,我試圖做的是從一個函數外部訪問一個變量(全部在同一個對象內)。從其範圍外訪問變量?
見下文:
function GameCard(imageSource, div)
{
this.cardImage = new Image();
this.cardImage.src = imageSource;
this.imageString = "<img src='" + this.cardImage.src + "' />";
this.hiddenImage = new Image();
this.hiddenImage.src = HIDDEN_SOURCE;
this.clicked = false;
this.cardDiv = div;
$(this.cardDiv).click(function() {
alert(this.imageString);
$(this).flip({
direction:'lr',
});
});
}
警報(我不幸調試)是說和imagestring是單擊處理函數中不確定的。這是有道理的,我將如何訪問它?
由於提前, Ĵ
它可能與一個範圍的問題之內上漲和訪問你的' this' .... – cclerville 2013-04-04 00:26:57
cclerville是正確的,當你在.click()函數中使用'this'時,它實際上是指被點擊的'cardDiv'對象,而不是封裝的'GameCard'對象。 – TheManWithNoName 2013-04-04 00:30:34