我很难在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 –