es6(12)--类,对象

时间:2023-03-09 19:29:08
es6(12)--类,对象
 //类,对象
{
//基本定义和生成实例
class Parent{
//定义构造函数
constructor(name='QQQ'){
this.name=name;
}
}
let v_parent=new Parent('v');
console.log(v_parent);
} {
//继承
class Parent{
//定义构造函数
constructor(name='QQQ'){
this.name=name;
}
}
class Child extends Parent{ }
console.log('继承',new Child())
}
{ //继承传递参数
class Parent{
//定义构造函数
constructor(name='QQQ'){
this.name=name;
}
}
class Child extends Parent{
constructor(name='child'){
super(name);//参数为空则会用父类的,需要覆盖父类,就要有参数
this.type='child';//super要放在第一行
}
}
console.log('继承传递参数',new Child('he')) }
{
//getter,setter
class Parent{
constructor(name='QQQ'){
this.name=name;
}
get longName(){
return 'mk'+this.name
}
set longName(value){
this.name=value;
}
}
let v=new Parent();
v.longName="hekk"
console.log('getter',v.longName);
}
{
//静态方法
class Parent{
constructor(name='QQQ'){
this.name=name;
}
//通过类去调用
static tell(){
console.log('tell')
}
} Parent.tell(); }
{
//静态属性
class Parent{
constructor(name='QQQ'){
this.name=name;
}
//通过类去调用
static tell(){
console.log('tell')
}
}
Parent.type='test';
console.log(Parent.type)
}