Answers for "hard copy object javascript"

12

copy object javascript

var x = {myProp: "value"};
var y = Object.assign({}, x);
Posted by: Guest on March-08-2020
3

javascript copy an object without reference

var obj = {a: 25, b: 50, c: 75};
var A = Object.create(obj);
var B = Object.create(obj);

A.a = 30;
B.a = 40;

alert(obj.a + " " + A.a + " " + B.a); // 25 30 40
Posted by: Guest on July-27-2020
1

javascript clone object

var x = {myProp: "value"};
var xClone = Object.assign({}, x);

//Obs: nested objects are still copied as reference.
Posted by: Guest on April-27-2020
-1

clone an object javascript

//returns a copy of the object
function clone(obj) {
    if (null == obj || "object" != typeof obj) return obj;
    var copy = obj.constructor();
    for (var attr in obj) {
        if (obj.hasOwnProperty(attr)) copy[attr] = obj[attr];
    }
    return copy;
}
Posted by: Guest on January-28-2020

Code answers related to "Javascript"

Browse Popular Code Answers by Language