类: 抽取对象(属性和行为)进行封装,成为一个类。

对象:类的实例化,获取类的对象。 

对象组成: 属性(数据)。 方法(函数)。

 

ES6  

1 声明类:class。

 class Name{}

2 实例化:new

3 类的constructor构造函数:

类的默认构造函数,用于传递参数,返回实例化对象。

在new的时候,自动调用,如果没有显示定义,类的内部会给我们自动创建一个constructor。

1,2,3例子

class Star{(类不像函数 没有())

constructor(uname){

this.uname =uname;

}

}

var ldh = new Star('liudh')

 liudh-> unmae形参-> unmae实参->this。this直向的是ldh。

 

4  类中添加方法:

 sing(){}

不需要写function(){},而且不需要写逗号分个。

 

5 类的继承:

class Father{}

class Son extends Father{}

 

6 由于 子类无法访问父类的this内容,导致函数调用参数失败。

 constructor(x,y){

 super(x,y);调用父类的构造函数。调用父类,father的constructor,

}

调用父类的构造函数:constructor(){ super();}

调用父类的普通函数: super.say();

查找规则:就近原则。

 

7 子类的super,必须放在this之前调用!!!!

this直向的对象。

 

8 类注意的两个点:

1 ES6中类没有遍历提升,必须先定义类,才能实例化对象。

2  类中,共有属性和方法,一定要this使用。

this.uname

this.sing()。

 

9 类中的this

1 constructor的this 指的是实例对象。

2 方法里面的this。 this指向的是方法的调用。

ldh.dance() (实例对象),this.btn.dance()(btn);

posted on 2021-01-17 22:11  程序员草莓  阅读(154)  评论(0编辑  收藏  举报