对象字面量

如果你想在对象里添加跟变量名一样的属性,并且属性的值就是变量表示的值就可以直接在对象里加上这些属性

1
2
3
4
5
6
7
8
9
10
11
let name = 'wjh';
let age = 8;
let getName = function(){
console.log(this.name)
}
let person ={
name,
age,
getName
}
person.getName();

7.2 Object.is

对比两个值是否相等

1
console.log(Object.is(NaN,NaN));

Object.assign

把多个对象的属性复制到一个对象中,第一个参数是复制的对象,从第二个参数开始往后,都是复制的源对象

1
2
3
4
5
6
7
8
9
10
var nameObj = {name:'wjh'}
var ageObj = {age:8};
var obj = {};
Object.assign(obj,nameObj,ageObj);
console.log(obj);

//克隆对象
function clone(obj){
return Object.assgin({},obj);
}

Object.setPrototypeOf

将一个指定的对象原型设置为另一个对象或者null

1
2
3
4
5
6
7
8
9
var obj1 = {name:'wjh1'};
var obj2 = {name:'wjh2'};
var obj = {};
Object.setPrototypeOf(obj,obj1);
console.log(obj.name);
console.log(Object.getPrototypeOf(obj));
Object.setProtoypeOF(obj,obj2);
console.log(obj.name);
console.log(Object.getPrototypeOf(obj));

proto

直接对象表达式中设置prototype

1
2
3
4
5
6
var obj1 = {name:'wjh'};
var obj3 = {
_proto_:obj1
}
console.log(obj3.name);
console.log(Object.getPrototypeOf(obj3));

super

通过super可以调用protype上的属性或方法

1
2
3
4
5
6
7
8
9
10
11
12
let person ={
eat(){
return 'milk';
}
}
let student = {
_proto_:person,
eat(){
return super.eat()+'bead'
}
}
console.log(student.eat());