ES6中Object的新特性


成员速写

成员速写是当返回的属性名和返回的值一致时可以简写

function create(userName, userAge) {
    return {
        userName,
        userAge
    }
}
console.log(create('robbit', 18))

// {userName: "robbit", userAge: 18}

方法速写

对象字面量初始化时可以简写

function create(userName, userAge) {
    return {
        userName,
        userAge,
        print() {
            console.log(this.userName, userAge)
        }
    }
}
var user = create('robbit', 18)
user.print()

// robbit 18

计算属性名

在我们从后台返回的参数我们不知道属性名,此时我们可以使用计算属性名。

const prop1 = 'userName';
const prop2 = 'userAge';
const prop3 = 'print';

const user = {
    [prop1]: 'robbit',
    [prop2]: 18,
    [prop3]() {
        console.log(this[prop1], this[prop2])
    }
}
console.log(user[prop1], user[prop2])
user[prop3]()

// user[prop1] => robbit
// user[prop2] => 18
// user[prop3] => robbit 18

is() 判断

判断两个值是否全等,除了一下两种情况。

console.log(NaN === NaN);
// false

console.log(Object.is(NaN, NaN))
// true
console.log(+0 === -0);
// true

console.log(Object.is(+0, -0))
// false

assign() 混合对象

用于混合对象,后值会覆盖前值。

const obj1 = {
    a: 123,
    b: "abc",
};
const obj2 = {
    b: 456,
    c: "def"
};
const obj = Object.assign(obj1, obj2)
console.log(obj)

// {a: 123, b: 456, c: "def"}

setPrototypeOf() 设置隐式原型

传递两个对象参数,将后一个对象设置为前一个对象的隐式原型。

const obj1 = {
    a: 1
}
const obj2 = {
    b: 2
}
Object.setPrototypeOf(obj1, obj2)
console.log(obj1)

// { a: 1,
//	 __proto__: {
//		b: 2
//	 }
// }

setPrototypeOf() 返回属性名

返回对象的所有自身属性的属性名(包括不可枚举属性但不包括Symbol值作为名称的属性)组成的数组。

const obj = {
    a: 1,
    b: 2,
    [Symbol("abc")]: 3
}
console.log(Object.getOwnPropertyNames(obj))

// ["a", "b"]

constructor 类的声明

class Person {
    constructor(job, name, age, sex) {
        this.job = job;
        this.name = name;
        this.age = age;
        this.sex = sex;
    }
    print() {
        console.log('工作:', this.job)
        console.log('姓名:', this.name)
        console.log('年龄:', this.age)
        console.log('性别:', this.sex)
    }
}
const a = new Person("程序猿", "robbit", 18, "男");
console.log(a)

在这里插入图片描述
类的声明不会提前,和let、const一样,有临时性死区
类的所有代码都在严格模式下执行的
类的所有方法都是不可枚举的

for (const prop in a) {
    console.log(prop)
}

// job
// name
// age
// sex

类的构造器必须使用 new 调用

const a = Person("程序猿", "robbit", 18, "男");

// TypeError: Class constructor Person cannot be invoked without 'new'

类的所有方法都无法当成构造函数直接使用

const b = new a.print()

// TypeError: a.print is not a constructor

getter 绑定属性

将对象属性绑定到查询该属性时将被调用的函数。

class Person {
    constructor(name) {
        this.name = name;
    }
    get age() {
        return 10
    }
    print() {
        console.log('姓名:', this.name)
        console.log('年龄:', this.age)
    }
}

var user = new Person('robbit')
console.log(user)

在这里插入图片描述


setter 设置属性

当尝试设置属性时,set语法将对象属性绑定到要调用的函数。

class Person {
    constructor(name, age) {
         this.name = name;
         this.age = age;
     }
    set age(age) {
        if (age < 0) {
            age = 0
        } else if (age > 200) {
            age = 200
        }
        this._age = age;
    }
    print() {
        console.log('姓名:', this.name)
        console.log('年龄:', this.age)
    }
}

var user = new Person('robbit', 300)
console.log(user)

// Person {name: "robbit", _age: 200}

setter和getter结合判断赋值

class Person {
    constructor(name, age) {
        this.name = name;
        this.age = age;
    }
    get age() {
        return this._age
    }
    set age(age) {
        if (age < 0) {
            age = 0
        } else if (age > 200) {
            age = 200
        }
        this._age = age;
    }
    print() {
        console.log('姓名:', this.name)
        console.log('年龄:', this.age)
    }
}

var user = new Person('robbit', 300)
console.log(user)

在这里插入图片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章