javascript - Make object not pass by reference -


For example:

I have just found a hard way by referencing objects in JavaScript, for example:

Layer = 0; Layer and Lieutenant; Hood Layer ['Layer'] Length; Layer ++) {// Store the values ​​calculated in this object var tempValues ​​= hudLayers ['Layers'] [layer]; TempValues ​​['name'] = 'test'; }

This will change the value in tempValues ​​and hudLayers. (Clearly, but a bit of code without a post seems so nude.)

Is there a quick way around this?

This is not an example of passing through context (you are not passing a benchmark). However, you are right; Specifying an object will not have any deep copy.

You can make a deep copy of an object like this:

  Function dot com (obje) {if (obof! == "object") obj; If obj.constructor === RegExp back; Var retVal = new obj.constructor (); (Var key in obj) {if (! Obj.hasOwnProperty (key)) release; Rated [key] = deep copy (obj [key]); } Return Rate Vals; }  

Note that if a constructor has any side-effects of an object, then this code will trigger them.


Comments