标签:rip 静态方法 bar tom com ret extends 函数式 super
function User(name,age) {
this.name=name
this.age=age
}
User.prototype.info=function(){
console.log(`my name is ${this.name}`)
}
const u1=new User(‘hk‘,22);
类的声明必须在使用之前,不像函数式的声明
class User{
}
或者
const User=class {
}
class User{
constructor(name,age){
this.name=name
this.age=age
}
info(){
console.log(`hello ${this.name}`)
}
static desc(){
console.log("version 11")
}
set github(value){
this.name=value
}
get github(){
return `https://github.com/${this.name}`;
}
}
const tom=new User("tom","20")
tom.github=‘tom‘
class Animal{
constructor(name){
this.name=name;
this.belly=[];
}
eat(food){
this.belly.push(food)
}
speak(){
console.log(`I am ${this.name}`)
}
}
class Dog extends Animal{
constructor(name,age){
super(name)
this.age=age
}
bark(){
console.log("bark bark!")
}
speak(){
console.log(`bark bark I am ${this.name}`)
}
}
const d1=new Dog(‘hk‘,18)
标签:rip 静态方法 bar tom com ret extends 函数式 super
原文地址:https://www.cnblogs.com/HKUI/p/13121061.html