导航
var obj = {a: 1}
var tar = {}
console.log(Object.assign(tar, obj))
console.log(tar === obj)
var obj = {
firstName: 'Yang'
}
var obj2 = {
lastName: 'Lance'
}
var copy = Object.assign(obj, obj2);
console.log(copy === obj)
const tar = {a: 1};
const tar1 = {b: 2, c: 2};
const tar2 = {c: 3};
Object.assign(tar, tar1, tar2);
console.log(tar);
var test = Object.assign(undefined, {a: 1})
var test = Object.assign(null, {a: 1})
var test1 = Object.assign(1, {a: 1})
var test2 = Object.assign(true, {a: 1})
var test3 = Object.assign('aa', {a: 1})
console.log(test1)
console.log(test2)
console.log(test3)
var test1 = Object.assign({a: 1}, undefined);
var test2 = Object.assign({a: 1}, 1);
var test3 = Object.assign({a: 1}, '123');
var test4 = Object.assign({a: 1}, true);
console.log(test1);
console.log(test2);
console.log(test3);
console.log(test4);
var test1 = 'abc';
var test2 = true;
var test3 = 10;
console.log(new Object(test1)) // 转为类数组,可以枚举
console.log(new Object(test2))
console.log(new Object(test3))
var obj = Object.create({foo: 1}, {
bar: {
value: 2
},
baz: {
value: 3,
enumerable: true
}
})
console.log(obj)
console.log(Object.assign({}, obj))
var test = Object.assign({a: 'b'}, {[Symbol('c')]: 'd'});
console.log(test);
var obj1 = {
a: {
b: 1
}
}
var obj2 = Object.assign({}, obj1);
obj1.a.b = 2;
console.log(obj1, obj2);
var tar = {a: {b: 1, c: 2}}
var source = {a: {b: 'hhhh'}}
console.log(Object.assign(tar, source))
console.log(Object.assign([1,2,3], [4,5]))
var source = {
get foo() {
return 1;
}
}
console.log(Object.getOwnPropertyDescriptor(source, 'foo'));
var tar = {}
Object.assign(tar, source);
console.log(tar);
function Person(){}
var age = 1;
Object.assign(Person.prototype, {
eat(){},
age
})
console.log(Person.prototype);
var obj = {}
Object.defineProperties(obj, {
a: {
value: true,
writable: true
},
b: {
value: 3
}
});
console.log(obj);
console.log(Object.getOwnPropertyDescriptors(obj));
var source = {
set foo(val) {
console.log(val)
}
}
var tar = {};
Object.assign(tar, source);
console.log(Object.getOwnPropertyDescriptors(tar));
var source = {
set foo(val){
console.log(val)
}
}
var tar = {};
Object.defineProperties(tar, Object.getOwnPropertyDescriptors(source));
console.log(Object.getOwnPropertyDescriptors(tar));
var obj = {a: 1, b: 3, c: {d: 5}}
var clone = Object.create(
Object.getPrototypeOf(obj),
Object.getOwnPropertyDescriptors(obj)
)
console.log(clone)
var obj = {a: 1, b: 3, c: {d: 5}}
var clone = Object.create(
Object.getPrototypeOf(obj),
Object.getOwnPropertyDescriptors(obj)
)
obj.c.d = 777
console.log(clone)