js note 7 category this, new, class, extends, super keywords

this keyword

Be a little lazy and use the teacher's courseware.

Call the function through the call() and apply() methods. The first actual parameter is the object pointed to by this, and the other actual parameters are passed to the formal parameters.

let obj1={a:1};
let obj2={a:2};
function f(c,d){
    console.log(this.a+c+d);
}
f.call(obj1,3,4);//8
f.apply(obj2,[3,4]);//9

new operator and constructor

new constructor name([arguments])

function Hotel(rooms,booked){
    this.rooms=rooms;
    this.booked=booked;
}
let orangeHotel=new Hotel(50,20);
let homeHotel=new Hotel(100,50);

The class keyword declares a class

Declaration syntax: class class name {class body}

Instantiation (generate object) syntax: new class name ()

Declare first, use later

class Hotel{
    constructor()
    {this.name='酒店';}
}
let obj=new Hotel();
obj.name;

Note: In es6, when creating each class class, there will be a constructor() method, which is used to create and initialize the special method of class creation object

extends keyword

class Home extends Hotel{
    selfMedthod(){;}

}
let obj=new Home(10,3);
obj.rooms;
obj.selfMedthod();

super keyword

class orange extends Hotel{
    constructor(rooms,booked){
    super(rooms,booked);
    this.pool=true;
}}

Guess you like

Origin blog.csdn.net/qq_59294119/article/details/124254624