一、构造函数和原型
1、构造函数
(1)作用:用来创建对象,初始化对象的属性
在es5中创建构造函数:
function Student(id,name){
this.id = id;
this.name = name;
}
let s1 = new Student(101,'张三')
在es6中创建类,给类单独定义构造函数
class Student {
constructor(id,name){
this.id = id;
this.name = name;
}
}
let s1 = new Student(101,'张三')//自动调用constructor函数
注意:通常把用构造函数创建对象的过程称为类的实例化,对象就是类的实例。
2、构造函数的成员或类的成员
(1)什么是成员?
成员包括属性和方法(行为),通常将属性称为成员变量,把方法或行为称为成员方法或成员函数。
(2)成员变量又称为实例变量:用类或构造函数创建的对象都具有相同的属性和方法
class Person{
constructor(id,name){
this.id = id;
this.name = name;
}
display(){
console.log('编号:'