我找到了这个例子,但我无法理解它是如何工作的:
function Stack() {
this.top = null;
}
Stack.prototype.push = function(val) {
this.top = {
data : val,
next : this.top
}
}
var S1 = new Stack();
S1.push(1);
S1.push(2);
console.log(S1);
为什么“next : this.top”解析为上一个推送的“this.top”对象?而不仅仅是返回 null?
最佳答案
当一个函数作为对象的方法被调用时,它的 this 被设置为调用该方法的对象。
你可以看到这个documentation了解更多详情
关于javascript - JavaScript 中的示例堆栈,需要澄清 'this' 的用法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41392455/