为什么我可以这样做:
Array.prototype.foo = function() {
this.splice(0, this.length);
return this.concat([1,2,3]);
}
但我不能这样做:
Array.prototype.foo = function() {
return this = [1,2,3];
}
这两个函数都会破坏 this 的值并将其更改为[1,2,3]
但第二个会引发以下错误:Uncaught ReferenceError: Invalid left-hand side in assignment
我怀疑这是因为允许分配意味着我可以将数组更改为其他内容(如字符串),但我希望那里有人肯定知道和/或有更详细的解释。