所以,我相对比较新的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