我很難在JS中圍繞變量作用域。有沒有類似於下面的例子訪問使用對象工廠創建的對象的實例變量的方法?如何訪問使用Javascript中的對象工廠創建的對象範圍中的方法
function Renderer(id, options) {
var id = id;
var options = options;
return {
render: function(selector) {
$(selector).each(function(index) {
this.renderOptions(); //This does not reference the Renderer, but the html element selected by jQuery.
});
},
renderOptions: function() {
console.log(this.options);
}
}
}
var myRenderer = new Renderer('test', [1, 2, 3, 5, 8, 13]);
您要訪問的ID和來自對象內部實例的選項? –
是的......最好我會在對象內部添加每個方法和變量的前綴以使其範圍更加明顯。 –
看到這個問題:http://stackoverflow.com/questions/10595888/best-practice-to-passing-context-to-callback-function –