【发布时间】:2013-01-22 17:59:30
【问题描述】:
这是我的代码
ImageCarousel = (function() {
var currentIndex, imageManager, imagesVO, jsonPath, values;
currentIndex = null;
jsonPath = "json/images.json";
imagesVO = [];
values = null;
imageManager = null;
function ImageCarousel() {
this.loadJson();
}
ImageCarousel.prototype.loadJson = function() {
var _this = this;
return $.ajax(jsonPath, {
success: function(data, status, xhr) {
console.log("yea " + data);
_this.currentIndex = 0;
_this.imagesVO = data.images;
_this.imageManager = new ImageManager(data.images);
_this.imagesCount = _this.imagesVO.length;
_this.switchToImage(_this.currentIndex);
$('#next').click(function() {
_this.currentIndex = _this.incrementIndexByOne(_this.currentIndex);
return _this.switchToImage(_this.currentIndex);
});
return $('#prev').click(function() {
_this.currentIndex = _this.decrementIndexByOne(_this.currentIndex);
return _this.switchToImage(_this.currentIndex);
});
},
error: function(xhr, status, err) {
return $('#imageHolder').html("problem loading the json file, </br>make sure you are running this on your local server");
},
complete: function(xhr, status) {}
});
};
我是否正确使用“this”来引用 ImageCarousel 类中的变量?它会公开这些属性吗?如果是这样,我如何将它们保密?
【问题讨论】:
-
你想如何使用
this?有几种使用this的方法,其中许多方法很糟糕。您可以通过不公开它们来创建私有属性。如果您将它们添加到this,您已经暴露了它们。 -
你不是说用
var代替_this吗?
标签: javascript jquery oop