對象的擴展
1.ES6中,對象的屬性和方法可簡寫:對象的屬性值可不寫,前提是屬性名已經聲明;
const name = "zhangsan";
const password = "2222222";
const obj = {
name,
password,
arr:[1,2,3,4],
sayName(){
console.log(this.name);
}
};
console.log(obj);
var obj1 = {
age:20,
};
2.Object.assign()方法
特點:淺拷貝、對象屬性的合併
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
|
Object.assign()方法的用法
-
合併多個對象
-
克隆對象(淺);
-
爲對象添加多個方法
-
Object.assign(SomeClass.prototype, {
someMethod(arg1, arg2) {
},
anotherMethod() {
}
});
// 原來的方法
SomeClass.prototype.someMethod = function (arg1, arg2) {
};
SomeClass.prototype.anotherMethod = function () {
};