ES6学习笔记13 类和对象

{
//基本定义和生成实例
class Parent{

constructor(name='laohan'){
this.name=name
}

}

let v_parent = new Parent()

console.log(v_parent)
}

{
//继承
class Parent{

constructor(name='laohan'){
this.name=name
}

}

class Child extends Parent{

}

let v_child = new Child()

console.log(v_child)
}

{
//继承传递参数
class Parent{

constructor(name='laohan'){
this.name=name
}

}

class Child extends Parent{

constructor(name='laohan2'){
super(name)
this.type='child'
}

}

let v_child = new Child('hello')

console.log('继承传递参数',v_child)
}

{
// getter,setter
class parent{

constructor(name='laohan'){
this.name=name
}

get longname(){
return 'mk'+this.name
}

set longname(value){
this.name=value
}

}

let v = new parent()
console.log('getter',v.longname)
v.longname='hello'
console.log('setter',v.longname)

}

{
// 静态方法
class parent{

constructor(name='laohan'){
this.name=name
}

static tell(){
console.log('tell')
}
}

parent.tell()
}

{
// 静态属性
class parent{

constructor(name='laohan'){
this.name=name
}

static tell(){
console.log('tell')
}
}

parent.type='test'

console.log('静态属性',parent.type)
}

猜你喜欢

转载自www.cnblogs.com/laohange/p/12815092.html
今日推荐