[removed] Store Object as Fixed Value?

后端 未结 3 558
清歌不尽
清歌不尽 2021-01-20 18:51

I\'ve noticed this behavior when writing my JavaScript, and I haven\'t been able to figure out why:

Below is some code to reproduce the behavior in question.

3条回答
  •  自闭症患者
    2021-01-20 18:56

    Because you are setting the objects to the same reference point. You need to clone the object. here is a piece of code from http://www.thespanner.co.uk/2008/04/10/javascript-cloning-objects/ that allows for cloning of objects with prototyping.

    Object.prototype.clone = function() {
      return eval(uneval(this));
    }
    alert("test".clone());
    alert((3).clone());
    alert(clone.clone());
    

提交回复
热议问题