“Klonobjekt in JS” Code-Antworten

Klonobjekt in JS

var student = {name: "Rahul", age: "16", hobby: "football"};

//using ES6
var studentCopy1 = Object.assign({}, student);
//using spread syntax
var studentCopy2 = {...student}; 
//Fast cloning with data loss
var studentCopy3 = JSON.parse(JSON.stringify(student));
JavascriptNinja

Objekt JavaScript kopieren

var x = {key: 'value'}
var y = JSON.parse(JSON.stringify(x))

//this method actually creates a reference-free version of the object, unlike the other methods
//If you do not use Dates, functions, undefined, regExp or Infinity within your object
slgotting

Objektklon JavaScript

const target = { a: 1, b: 2 };
const source = { b: 4, c: 5 };

const returnedTarget = Object.assign(target, source);

console.log(target);
// expected output: Object { a: 1, b: 4, c: 5 }

console.log(returnedTarget);
// expected output: Object { a: 1, b: 4, c: 5 }
Curious Chimpanzee

JavaScript -Klonobjekt

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

//Obs: nested objects are still copied as reference.
Eranot

Klon ein Objekt 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;
}
QuietHumility

Ähnliche Antworten wie “Klonobjekt in JS”

Fragen ähnlich wie “Klonobjekt in JS”

Weitere verwandte Antworten zu “Klonobjekt in JS” auf JavaScript

Durchsuchen Sie beliebte Code-Antworten nach Sprache

Durchsuchen Sie andere Codesprachen