鍍金池/ 問答/Java  網(wǎng)絡(luò)安全  HTML/ JS把某個(gè)對(duì)象復(fù)制給另外一個(gè)對(duì)象,報(bào)了Invalid left-hand sid

JS把某個(gè)對(duì)象復(fù)制給另外一個(gè)對(duì)象,報(bào)了Invalid left-hand side in assignment

JS把某個(gè)對(duì)象復(fù)制給另外一個(gè)對(duì)象,報(bào)了Invalid left-hand side in assignment。

在代碼的17行,我想把這個(gè)對(duì)象的左邊一個(gè)對(duì)象復(fù)制給這個(gè)對(duì)象,但是始終搞不定

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(){
        //這一行總是報(bào)錯(cuò) Invalid left-hand side in assignment
        this = this.left();
    }
}

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

我哪里出了問題呢?

回答
編輯回答
九年囚

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

2017年10月31日 10:01
編輯回答
離殤

this對(duì)象時(shí)不能被顯式賦值的,所以會(huì)報(bào)Invalid left-hand side in assignment 非法的左賦值

2017年8月12日 01:11
編輯回答
心沉

Uncaught ReferenceError: Invalid left-hand side in assignment
同類錯(cuò)誤:

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

當(dāng)嘗試給一個(gè)不能被賦值的變量賦值時(shí)將發(fā)生該錯(cuò)誤??聪旅娴牡湫屠樱?/p>

if(doSomething() = 'somevalue')

在上面例子中,開發(fā)人員不小心將 == 寫成了 =,錯(cuò)誤消息“l(fā)eft-hand side in assignment”指等號(hào)左邊包含不能被賦值的變量。

如何修復(fù):確保不給函數(shù)函數(shù)的返回值或 this 關(guān)鍵字賦值。

另外, 試了下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(){
        //這一行總是報(bào)錯(cuò) 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

2017年3月17日 05:35
編輯回答
扯機(jī)薄

關(guān)于

PS:如果我有一千個(gè)屬性呢? 我試了一下 hasOwnProperty(),但是始終返回false

建議你貼一下你的代碼,估計(jì)是你用錯(cuò)了方法,hasOwnProperty是用于判斷是否有某個(gè)屬性,而不是枚舉所有屬性的,此外它還不能枚舉原型鏈上的屬性。如果想判斷原型鏈上的屬性,需要用 in 操作符。

2018年4月19日 19:22
編輯回答
有你在

沒看出來你為什么要寫的這么復(fù)雜

Point.prototype = {
    constructor : Point,
    left : function(){
        if(this.x > 0)
        {
            this.x--;
        }
    },
    moveL : function(){
        this.left();
    }
}
2018年2月9日 23:04
編輯回答
疚幼

用以下代碼替換17行,可以達(dá)到復(fù)制的目的

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

但是,對(duì)象的prototype屬性也被復(fù)制到了 this 中,所以,我自己寫了一個(gè)方法

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

那么,調(diào)用如下:

copy(this.left(),this);

這么一來,我的問題解決了。

PS:如果我有一千個(gè)屬性呢? 我試了一下 hasOwnProperty(),但是始終返回false,期待高人指點(diǎn)

2017年1月29日 09:55
編輯回答
貓館
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函數(shù)

2017年3月8日 21:05