ES6之Class篇
ES6 Class声明
class Animal{
constructor(type){
this.type=type
}
eat(){
console.log('i am eating')
}
}
let dog=new Animal('dog')
console.log(dog)
dog.eat()
typeof Animal
ES6属性的读写操作
let _age=4
class Animal{
constructor(type){
this.type=type
}
get age(){
return _age
}
set age(val){
_age=val
}
eat(){
console.log('i am eating')
}
}
let dog=new Animal('dog')
console.log(dog.age)
dog.age=5
console.log(dog.age)
console.log(dog._age)
ES6静态方法
class Animal{
constructor(type){
this.type=type
}
eat(){
Animal.walk()
console.log('i am eating')
}
static walk(){
console.log('i am walking')
}
}
let dog=new Animal('dog')
dog.eat()
ES6继承
class Animal{
constructor(type){
this.type=type
}
eat(){
Animal.walk()
console.log('i am eating')
}
static walk(){
console.log('i am walking')
}
}
class Dog expend Animal{
constructor(type){
super(type)
this.age=2
}
}
let dog=new Dog('dog')
dog.eat()