学习笔记:ES6之类和对象

来源:互联网 发布:windows 直接登录 编辑:程序博客网 时间:2024/06/08 09:58

类的概念

  • 基本语法

//类的基本定义和生成实例

//类的定义

classParent{

constructor(name='keke'){

this.name=name;

}

}

//生成实例

letv_parent1=newParent();//默认name:keke

letv_parent2=newParent('nono');//name:nono

 

  • 类的继承

//继承

classParent{

constructor(name='keke'){

this.name=name;

}

}

classChildextendsParent{

 

}

letchild=newChild();//name:keke

//继承传递参数

classParent{

constructor(name='keke'){

this.name=name;

}

}

classChildextendsParent{

constructor(name='child'){

super(name);//super(参数):参数为空,则默认使用父类的值;参数不为空,则传递自己的参数

this.type='child';

}

}

letchild=newChild();//name:child---存在super(name)

letchild=newChild();//name:keke---不存在super(name)

 

  • 静态方法

  •        --通过类去调用,而不是通过类的实例去调用

//静态方法

classParent{

constructor(name='keke'){

this.name=name;

}

//静态方法的定义

statictell(){

console.log('tell');

}

}

Parent.tell();//tell

  • 静态属性

//静态属性

classParent{

constructor(name='keke'){

this.name=name;

}

statictell(){

console.log('tell');

}

}

Parent.type='text';//设置静态属性

console.log(Parent.type);//text

 

  • Getter和setter属性

//getter,setter

classParent{

constructor(name='keke'){

this.name=name;

}

getlongName(){

return'mk'+this.name;

}

setlongName(value){

this.name=value;

}

}

letv=newParent();

console.log(v.longName);//mkkeke

v.longName='hello';//赋值相当于setter

console.log(v.longName);//mkhello

 

原创粉丝点击