Javascript 关闭不起作用
在上述情况下,我预期的输出为: “你好,世界”Javascript 关闭不起作用,javascript,Javascript,在上述情况下,我预期的输出为: “你好,世界” 为什么this.\u实例没有访问应该通过闭包访问的变量?我在FF中对此进行了测试,但没有定义。您的问题是此 更改此。_实例为_实例。您可能还希望将自调用函数封装在括号中,如(function(){…})()用于最大限度的浏览器兼容性。您没有将\u实例分配给对象,它只是一个闭包变量,应该在不使用此的情况下访问: var Dog = function() { var _instance = 'hello world'; return
为什么
this.\u实例
没有访问应该通过闭包访问的变量?我在FF中对此进行了测试,但没有定义。您的问题是此
更改此。_实例
为_实例
。您可能还希望将自调用函数封装在括号中,如(function(){…})()代码>用于最大限度的浏览器兼容性。您没有将\u实例
分配给对象,它只是一个闭包变量,应该在不使用此
的情况下访问:
var Dog = function() {
var _instance = 'hello world';
return function() {
console.log(this._instance);
}
} (); //note that it is self invoking function
var l = new Dog(); //#> undefined
我可能会这样写:
var Dog = function() {
var _instance = 'hello world';
return function() {
console.log(_instance);
}
} (); //note that it is self invoking function
var l = new Dog();
请注意,我没有尝试编译上述代码,因此它可能包含一些错误。不过,没有什么不能处理的
您可能需要考虑将过滤添加到StestOp选项方法中,这样它就不会分配您不想要的属性,或者筛选出在Op选项参数中声明的方法等。
此外,如果使用JQuery或类似的库,则(通常)会有用于合并对象的实用程序函数,因此编写setOptions方法非常简单:
var Dog = (function() {
var defaults = {
name: 'Rags'
};
var Dog = function (options) {
// Take options as a constructor argument, this
// allows you to merge it with defaults to override
// options on specific instances
this.setOptions(options);
};
Dog.prototype = {
// Common methods for Dogs here
setOptions: function (options) {
// Declare all variables in the beginning of the method because
// JavaScript hoists variable declarations
var key = null;
// First assign all the defaults to this object
for ( key in defaults) {
this[key] = defaults[key];
}
// Now override with the values in options:
if (options && options.hasOwnProperty) {
for ( key in options ) {
this[key] = options[key];
}
}
}
};
return Dog; // Return the constructor method
} ()); // wrap the self-invoked function in paranthesis to visualize that
// it creates a closure
var buster = new Dog({name: 'Buster'}),
unnamed = new Dog();
alert(buster.name); // Alerts 'Buster'
alert(unnamed.name); // Alerts 'Rags'
正如其他人所说,您需要从函数中删除“this.”
问题的原因在于两个函数上下文中“this”关键字的绑定。在闭包内部,“this”指的是正在返回的函数,而不是外部函数。您可以通过执行以下操作来解决此问题:
function setOptions (options) {
// I assume JQuery here
// true as the first argument gives us a recursive merge
var mergedOptions = $.extend(true, defaults, options);
for (var key in mergedOptions ) {
if(this.checkAllowedProperty(key, typeof(mergedOptions[key])) {
this[key] = mergedOptions[key];
}
}
}
/**
* This method checks if propertyName is an allowed property on this object.
* If dataType is supplied it also checks if propertyName is allowed for
* dataType
* @return true if propertyName, with type dataType, is allowed on this object,
* else false
*/
function checkAllowedProperty (propertyName, dataType);
您也可以使用function.apply()方法做一些更接近的事情,但我将把这留给您
我希望这会有所帮助。也许您对删除“this”感到满意,但您可能有兴趣了解到,“this”并不是指您想要的内容。它所指的内容实际上取决于函数的调用方式。它不一定引用由返回的函数构造的对象的实例,或其容器函数,或任何其他对象。默认情况下,如果仅将函数作为普通函数调用,“this”将引用全局窗口上下文
要将“this”绑定到任何特定对象,必须将函数作为该对象或其原型的方法调用。e、 g.foo.myMethod()。另一种方法是,您可以使用apply或call方法,传入要应用它的对象。e、 g.anyFunction.apply(foo)。+1-如果我花时间解释我的答案,它会掩盖一个微妙的书呆子笑话。这台机器太简单了,代码记录得这么好,我不得不给他打分。希望我能分几点来获得详细的答案和速度。:-)@多雷拉尔-没问题,我只是需要在我的答案上投入更多的精力。
var Dog = function() {
var _instance = 'hello world';
var that = this; //Assign "this" to "that"
return function() {
console.log(that._instance); //Use reference to "that"
}
} ();
var l = new Dog();