ES6-introduction of class

ES6-introduction of class

ES6 provides a way of writing closer to traditional languages, and introduces the concept of class as a template for objects. Through the class keyword, the writing of object prototypes can be made clearer and more like object-oriented programming syntax.

  1. The class declares the class, and the constructor defines the constructor initialization
		class Phone1 {
            constructor(brand, price) {
                this.brand = brand;
                this.price = price;
            }
            call() {
                console.log('我能打电话');
            }
        }
        let onePlus = new Phone1('1+', 1999);
        console.log(onePlus);

Here is the difference with the constructor

function Star(name,age,gender){
	this.name=name;
	this.age=age;
	this.gender=gender;
}
Star.prototype.talent=function(){
	console.log('我会演小品');
}
let bp1=new Star('jisoo',25,'女');
console.log(bp1);
Star.talent();
  1. extends inherits the parent class, super calls the parent construction method
		class Phone {
            constructor(brand, price) {
                this.brand = brand;
                this.price = price;
            }
            call() {
                console.log('我能拍照');
            }
        }
        class SmartPhone extends Phone {//Phone为要继承的父类
            constructor(brand, price, color, size) {
                super(brand, price);//super里面的属性为继承父类的属性,新的属性在外面补充
                this.color = color;
                this.size = size;
            }
            photo() {
                console.log('我能拍照');
            }
        }
        let xiaomi = new SmartPhone('xiaomi', 1999, 'red', 15.6);
        console.log(xiaomi);
        xiaomi.call();
  1. static defines static methods and properties
		/* class静态成员 */
        class Star {
            //静态属性
            static name = 'jisoo';
            static habit = function() {
                console.log("That's a pity");
            }
        }
        let jisoo = new Star();
        console.log(Star.name); //jisoo
        console.log(jisoo.name); //undefined
  1. The parent class method can be overridden
		class Father {
            constructor(name, age) {
                this.name = name;
                this.age = age;
            }
            money() {
                console.log('我能赚钱');
            }
        }
        class Son extends Father {
            constructor(name, age, gender) {
                super(name, age);
                this.gender = gender;
            }
            money() {
                console.log('我也能赚钱');
            }
        }
        let son = new Son('jisoo', 25, '女');
        console.log(son);
        son.money();

Guess you like

Origin blog.csdn.net/Angela_Connie/article/details/114239023