“JavaScript Object.assign” Kode Jawaban

JavaScript

const obj = { a: 1 };
const copy = Object.assign({}, obj);
console.log(copy); // { a: 1 }
Cheerful Cod

Salin objek JavaScript

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

JavaScript Object.assign

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

MDN Object Assign

The Object.assign() method copies all enumerable own properties from one or more source objects to a target object. It returns the target object.
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 }
Worrisome Wallaby

Objek yang ditugaskan dalam JavaScript

// Object assign in javascript
let fname = { firstName : 'Black' };
let lname = { lastName : 'Panther'}
let full_names = Object.assign(fname, lname);
console.log(full_names); //{ firstName: 'Black', lastName: 'Panther' }
Chetan Nada

Objek.assign ()

const obj1 = {
				a: 5,
                b: 2
                }
const obj2 = Object.assign({a:6 d:7}, obj1);

console.log(obj2);
// output:  { a: 5 , b: 2, d:7} 
DLiTeCoder

Jawaban yang mirip dengan “JavaScript Object.assign”

Pertanyaan yang mirip dengan “JavaScript Object.assign”

Lebih banyak jawaban terkait untuk “JavaScript Object.assign” di JavaScript

Jelajahi jawaban kode populer menurut bahasa

Jelajahi bahasa kode lainnya