ES6中新增class类

我们都知道JavaScript是弱类型的语言,其缺陷就是缺乏严谨性,但是在JavaScript发展的过程中需要慢慢的弥补这些缺陷,于是便在ES6中出现了类的概念。

什么是类?

类是具备某些共同特征的实体的集合,它是一种抽象的概念。

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)

在这里插入图片描述


extends 继承

class Animal {
    constructor(type, name, age, sex) {
        this.type = type;
        this.name = name;
        this.age = age;
        this.sex = sex;
    }
    print() {
        console.log(`种类 : ${this.type}`)
        console.log(`名字 : ${this.name}`)
        console.log(`年龄 : ${this.age}`)
        console.log(`性别 : ${this.sex}`)
    }
}
class Dog extends Animal {
   
    constructor(name, age, sex) {
    	 // 如果定义了constructor 并表示这个是子类,则必须在constructor的第一行手动调用父类的构造函数
        super("犬类", name, age, sex) 
    };
}

const d = new Dog("旺财", 5, "公");

// Dog {type: "犬类", name: "旺财", age: 5, sex: "公"}

super

如果定义了constructor 并表示这个是子类,则必须在constructor的第一行手动调用父类的构造函数,但是super如果说当成对象使用,则表示父类的原型。

class Animal {
    constructor(type, name, age, sex) {
        this.type = type;
        this.name = name;
        this.age = age;
        this.sex = sex;
    }
    print() {
        console.log(`种类 : ${this.type}`)
        console.log(`名字 : ${this.name}`)
        console.log(`年龄 : ${this.age}`)
        console.log(`性别 : ${this.sex}`)
    }
}
class Dog extends Animal {
    constructor(name, age, sex) {
        super("犬类", name, age, sex);
        this.loves = "吃骨头"
    }
    print() {
        super.print();
        console.log(`爱好 : ${this.loves}`)
    }
}

const d = new Dog("旺财", 5, "公");
console.log(d);

// Dog {type: "犬类", name: "旺财", age: 5, sex: "公", loves: "吃骨头"}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值