{
class Parent {
constructor(name = 'binbin'){
this.name = name;
}
}
let parent = new Parent('v');
console.log('构造函数和实例',parent);
}
输出结果:构造函数和实例 Parent {name: "v"}
{
//继承
class Parent{
constructor(name='binbin'){
this.name = name;
}
}
class Child extends Parent{
constructor(name = 'child'){
super(name);
this.type = 'child';
}
}
console.log('继承传递参数',new Child('hello'));
}
输出结果:继承传递参数 Child {name: "hello", type: "child"}
{
// getter,setter
class Parent{
constructor(name='binbin'){
this.name = name;
}
get longName(){
return 'wang' + this.name;
}
set longName(value){
this.name = value;
}
}
let parent = new Parent();
console.log('getter',parent.longName);
parent.longName = 'li';
console.log('setter',parent.longName);
}
输出结果:getter wangbinbin
setter wangli
{
// 静态属性
class Parent{
constructor(name="binbin"){
this.name = name;
}
static tell(){
console.log('tell');
}
}
Parent.tell(); //采用类调用,并非new Parent()实例调用
Parent.type = 'vava';
console.log(Parent.type);
}
输出结果:tell
vava
网友评论