Javascript variables keep track to their copies?

This question already has an answer here:

  • What is the most efficient way to deep clone an object in JavaScript? 57 answers
  • Is JavaScript a pass-by-reference or pass-by-value language? 29 answers

  • The get method in the proceeding line returns a reference to the layerIds object within the model:

    var d = this.model.get('layerIds');
    

    When the maplayer property on d is set the reference is also manipulated. Basically d and this.model.get('layerIds') will return the same object from memory.

    d.mapLayer = view.getId();
    

    If you checked equality between the two you will notice they are the same object.

    d === this.model.get('layerIds') // true


    From Javascript by reference vs. by value:

    "Javascript is always pass by value, but when a variable refers to an object (including arrays), the "value" is a reference to the object."

    this.model.get('layerIds'); //  returns an object : {mapLayer: null}
    
    链接地址: http://www.djcxy.com/p/20818.html

    上一篇: 为什么这个javascript表达式被评估为false?

    下一篇: Javascript变量跟踪他们的副本?