javascript - 引用错误 : Invalid left-hand side in assignment when really assigning a value

标签 javascript node.js this variable-assignment

为什么这个片段在 Node (10.5)

    .then(function() {
      this = {...this, ...payload};
      this.update();
      resolve({ok:true, node});
    });

出现以下错误:

ReferenceError: Invalid left-hand side in assignment

有效负载包含一些需要添加到 this 的属性,或者如果属性存在,则需要更新该属性。

我不明白为什么会出现这个错误:(

最佳答案

嗯,错误是直截了当的,它说 Invalid left-hand side in assignment,这意味着您在赋值的左侧使用了错误的元素。

那是因为您正在编写 this = {...this, ...payload};,您试图将值写入 this。在游览功能中,这是错误的,也是不可能的,因为您不能更改 this 并为其分配一个值,因为它在JavaScript。

如果您检查 MDN this Reference你可以看到:

In most cases, the value of this is determined by how a function is called. It can't be set by assignment during execution, and it may be different each time the function is called.

关于javascript - 引用错误 : Invalid left-hand side in assignment when really assigning a value,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51097728/

相关文章:

PHP Javascript 变量帮助

javascript - 用键将数组插入数组

node.js - npm install fibers 显示错误

javascript - Angular 5 中 Observable 中 'this' 的范围

jquery - 将 jQuery 函数应用于单独出现的元素

javascript - Discord 机器人 - 跨 channel 沟通

javascript - 当事件发生在另一个可观察量中时,从一个可观察量中检索值

javascript - 等待正常功能

node.js - 让 Sails 使用正确的 CoffeeScript 版本

jquery - 为什么 "$(this)"不能在 jquery-backstretch 上工作宽度 .data?