ES6-Class的写法

========1.Class的用法========
class Person{
constructor(name, age){
this.name = name;
this.age = age;
}
say(words){
console.log(`${this.name} is saying ${words}.`);
}
}

let p = new Person('小王', 25);
p.say('hello world');
 
================运行结果====================
 

小王 is saying hello world.

 

 

================分割线====================

========2.立即执行函数的Class========

let p = new class {
constructor(name, age){
this.name = name;
this.age = age;
}
say(words){
console.log(`${this.name} is saying ${words}.`);
// console.log(Person.name);
}
}('小王', 30);
p.say('hello');
 
================运行结果====================
 
 

小王 is saying hello.

 

================分割线====================

========3.class不会进行变量提升,所有的class,必须先定义后使用========

 
console.log(Person);
class Person{
constructor(){

}
}
 
================运行结果====================
 

ReferenceError: Cannot access 'Person' before initialization

posted @ 2021-02-10 11:12  #Friday  阅读(123)  评论(0)    收藏  举报