为什么';我的目标是';JavaScript中的属性更新?
我有一个函数,我已经为构造函数调用准备好了 这些方法是在为什么';我的目标是';JavaScript中的属性更新?,javascript,Javascript,我有一个函数,我已经为构造函数调用准备好了 这些方法是在Queue的原型属性上设置的……一切正常吗 Queue.prototype.enqueue = function(name, options) { this.capacity[name] = options || {}; this.count(); if (this._count > 5) { return 'Max capacity has been reached—which is five
Queue
的原型属性上设置的……一切正常吗
Queue.prototype.enqueue = function(name, options) {
this.capacity[name] = options || {};
this.count();
if (this._count > 5) {
return 'Max capacity has been reached—which is five, please dequeue....'
}
};
Queue.prototype.count = function() {
var total = Object.keys(this.capacity);
total.forEach(function(elem) {
this._count++
});
if (this._count == 1) {
console.log(this.capacity[Object.keys(this.capacity)])
console.log( 'There is one item in the queue');
} else {
console.log(this.capacity[Object.keys(this.capacity)])
console.log( 'There are ' + this._count + ' items in the queue');
}
};
我的问题是,当enqueue/count方法触发时,如何使计数增加?我不断得到:
There are 0 items in the queue
我知道我可以将它添加到.prototype
属性中,并将其放在count函数中,让它引用本地变量
Queue.prototype.count = function() {
var total = Object.keys(this.capacity), count = 0;
total.forEach(function(elem) {
this.count++
});
Queue.prototype.call=call/您需要在forEach中绑定它
Queue.prototype.count = function() {
var total = Object.keys(this.capacity);
total.forEach(function(elem) {
this._count++
}.bind(this)); //bind the context
if (this._count == 1) {
console.log(this.capacity[Object.keys(this.capacity)])
console.log( 'There is one item in the queue');
} else {
console.log(this.capacity[Object.keys(this.capacity)])
console.log( 'There are ' + this._count + ' items in the queue');
}
};
您需要在forEach中绑定此文件
Queue.prototype.count = function() {
var total = Object.keys(this.capacity);
total.forEach(function(elem) {
this._count++
}.bind(this)); //bind the context
if (this._count == 1) {
console.log(this.capacity[Object.keys(this.capacity)])
console.log( 'There is one item in the queue');
} else {
console.log(this.capacity[Object.keys(this.capacity)])
console.log( 'There are ' + this._count + ' items in the queue');
}
};
尝试以下修改(绑定函数):
问题在于
this
引用的对象与父函数中的对象不同,因为在JS中,闭包不保留this
,而是由调用方决定this
值。或者,您可以使用foreach的第二个thisArg
参数。尝试以下修改(绑定函数):
问题在于
this
引用的对象与父函数中的对象不同,因为在JS中,闭包不保留this
,而是由调用方决定this
值。或者,您可以使用foreach的第二个thisArg
参数。现有的答案为问题本身提供了很好的解决方案,我只是想详细说明一下原因
此
是由执行上下文指定的引用。更清楚地说,它是由函数的调用位置确定的引用。由于可以像其他任何值一样在JavaScript中传递函数,这可能会导致由于引用是移动目标而导致的问题
代码的问题是,您在forEach
中引用了此
forEach
将函数作为参数并调用它,因为此指向的是函数的调用位置,而不是函数的定义位置。调用函数时,值会有所不同。如果处于严格模式,它最终会返回到任何全局上下文或未定义的上下文
有许多不同的方法来处理这个问题
您可以将对外部this
的引用存储在变量上,并在另一个函数中使用它
var self = this;
total.forEach(function(elem) {
self._count++;
});
您可以使用.bind
。这是一个函数方法,它返回一个函数,该函数使用传入的对象作为此
的引用,无论您在哪里调用它
total.forEach(function(elem) {
this._count++;
}.bind(this));
或者你可以使用箭头功能。箭头函数不会创建自己的上下文,因此它们只会从周围的上下文中维护this
的值
total.forEach(() => {
this._count++;
});
这是一个常见的问题,这些都是有效的解决方案。在我看来,它们从最少到最优雅。现有的答案为问题本身提供了很好的解决方案,我只是想我应该详细说明一下原因
此
是由执行上下文指定的引用。更清楚地说,它是由函数的调用位置确定的引用。由于可以像其他任何值一样在JavaScript中传递函数,这可能会导致由于引用是移动目标而导致的问题
代码的问题是,您在forEach
中引用了此
forEach
将函数作为参数并调用它,因为此指向的是函数的调用位置,而不是函数的定义位置。调用函数时,值会有所不同。如果处于严格模式,它最终会返回到任何全局上下文或未定义的上下文
有许多不同的方法来处理这个问题
您可以将对外部this
的引用存储在变量上,并在另一个函数中使用它
var self = this;
total.forEach(function(elem) {
self._count++;
});
您可以使用.bind
。这是一个函数方法,它返回一个函数,该函数使用传入的对象作为此
的引用,无论您在哪里调用它
total.forEach(function(elem) {
this._count++;
}.bind(this));
或者你可以使用箭头功能。箭头函数不会创建自己的上下文,因此它们只会从周围的上下文中维护this
的值
total.forEach(() => {
this._count++;
});
这是一个常见的问题,这些都是有效的解决方案。在我看来,它们从最小到最优雅。或者使用带有此绑定的箭头函数,或者简单地执行过时的
var self=this编码>并在回调中使用self
。同意。有很多解决方案。但我猜用户希望解决方案在IE10,IE11中工作。另一方面,他可能会在任何地方使用ES6语法,或者使用带有词法绑定的箭头函数,或者简单地执行过时的var self=this编码>并在回调中使用self
。同意。有很多解决方案。但我猜用户希望解决方案在IE10,IE11中工作。另一方面,他会在任何地方使用ES6语法。你可以简化为this.\u count+=Object.keys(this.capacity).length
。你不是想把\u count
重置为零,或者用=
代替+=
?你可以简化为这个。\u count+=Object.keys(this.capacity)。length
。你的意思是不是要将计数重置为零,或者使用=
而不是+=
?