JS把某个对象复制给另外一个对象,报了Invalid left-hand side in assignment

发布于 2022-09-06 09:21:19 字数 607 浏览 19 评论 0

JS把某个对象复制给另外一个对象,报了Invalid left-hand side in assignment。

在代码的17行,我想把这个对象的左边一个对象复制给这个对象,但是始终搞不定

var MATRIX = 5;
var Point = function(x,y){
    this.x = x;
    this.y = y;
    this.nth = (this.y*MATRIX) + this.x;
    return this;
}
Point.prototype = {
    constructor : Point,
    left : function(){
        if(this.x > 0){
            return new Point(this.x-1,this.y);
        }
    },
    moveL : function(){
        //这一行总是报错 Invalid left-hand side in assignment
        this = this.left();
    }
}

var p = new Point(2,3);
p.left();
p.moveL();

我哪里出了问题呢?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

权谋诡计 2022-09-13 09:21:19
var MATRIX = 5;

function extendTo (base, receiver) {
    var receiver = receiver || {};
    for(var prop in base) {
        if ( base.hasOwnProperty(prop)) {
            receiver[prop] = base[prop];
        }
    }
    return receiver;
}

var Point = function(x,y){
    this.x = x;
    this.y = y;
    this.nth = (this.y*MATRIX) + this.x;
    return this;
}
Point.prototype = {
    constructor : Point,
    left : function(){
        if(this.x > 0){
            return new Point(this.x-1,this.y);
        }
    },
    moveL : function(){
        extendTo(this.left(), this)
    }
}

var p = new Point(2,3);
console.log(p.x, p.y, p.nth);   //2,3,17

p.moveL();
console.log(p.x, p.y, p.nth);   //1,3,16

你可以看下我的extendTo函数

情绪操控生活 2022-09-13 09:21:19

this对象时不能被显式赋值的,所以会报Invalid left-hand side in assignment 非法的左赋值

拍不死你 2022-09-13 09:21:19

Uncaught ReferenceError: Invalid left-hand side in assignment
同类错误:

Uncaught exception: ReferenceError: Cannot assign to ‘functionCall()’
Uncaught exception: ReferenceError: Cannot assign to ‘this’

当尝试给一个不能被赋值的变量赋值时将发生该错误。看下面的典型例子:

if(doSomething() = 'somevalue')

在上面例子中,开发人员不小心将 == 写成了 =,错误消息“left-hand side in assignment”指等号左边包含不能被赋值的变量。

如何修复:确保不给函数函数的返回值或 this 关键字赋值。

另外, 试了下copy,hasOwnProperty()返回true:

var MATRIX = 5;
var Point = function(x,y){
    this.x = x;
    this.y = y;
    this.nth = (this.y*MATRIX) + this.x;
    return this;
}
Point.prototype = {
    constructor : Point,
    left : function(){
        if(this.x > 0){
            return new Point(this.x-1,this.y);
        }
    },
    moveL : function(){
        //这一行总是报错 Invalid left-hand side in assignment
        copy(this.left(),this)
    }
}
function copy(p, c) {
    var c = c || {};
    c.x = p.x;
       c.y = p.y;
       c.nth = p.nth;
    return c;
}
var p = new Point(2,3);
p.moveL()

console.log(p.hasOwnProperty("x"),p.hasOwnProperty("y"),p.hasOwnProperty("nth"))

// true true true

送舟行 2022-09-13 09:21:19

用以下代码替换17行,可以达到复制的目的

$.extend(this,this.left());

但是,对象的prototype属性也被复制到了 this 中,所以,我自己写了一个方法

function copy(p, c) {
    var c = c || {};
    c.x = p.x;
       c.y = p.y;
       c.nth = p.nth;
    return c;
}

那么,调用如下:

copy(this.left(),this);

这么一来,我的问题解决了。

PS:如果我有一千个属性呢? 我试了一下 hasOwnProperty(),但是始终返回false,期待高人指点

挖鼻大婶 2022-09-13 09:21:19

关于

PS:如果我有一千个属性呢? 我试了一下 hasOwnProperty(),但是始终返回false

建议你贴一下你的代码,估计是你用错了方法,hasOwnProperty是用于判断是否有某个属性,而不是枚举所有属性的,此外它还不能枚举原型链上的属性。如果想判断原型链上的属性,需要用 in 操作符。

何以心动 2022-09-13 09:21:19

没看出来你为什么要写的这么复杂

Point.prototype = {
    constructor : Point,
    left : function(){
        if(this.x > 0)
        {
            this.x--;
        }
    },
    moveL : function(){
        this.left();
    }
}
是你 2022-09-13 09:21:19

Object.assign(this, this.left())

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文