在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

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

JS把某個對象復制給另外一個對象,報了Invalid left-hand side in assignment

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();

我哪里出了問題呢?

回答
編輯回答
九年囚

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

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

this對象時不能被顯式賦值的,所以會報Invalid left-hand side in assignment 非法的左賦值

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

Uncaught ReferenceError: Invalid left-hand side in assignment
同類錯誤:

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

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

if(doSomething() = 'somevalue')

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

如何修復:確保不給函數(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(){
        //這一行總是報錯 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
編輯回答
扯機薄

關(guān)于

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

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

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

沒看出來你為什么要寫的這么復雜

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

用以下代碼替換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;
}

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

copy(this.left(),this);

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

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

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