//ES5中
function Person(){ //实例方法
this.run1 = function(){}
}
Person.run2 = function(){ //静态方法
}
var p = new Person() //实例方法
Person.run2() //静态方法调用
//TS中
class Person{
public name:string;
static sex = '男' //静态属性
constructor(name:string){
this.name=name;
}
run(){ //实例方法
console.log(`${this.name}在运动`)
}
work(){
console.log(`${this.name}在工作`)
}
static prin(){ //静态方法
console.log(`静态方法`) //注意这里无法直接使用方法中this.name,sex可以调用Person.sex
}
}
var p = new Person('梁豆豆')
p.run() //实例方法调用
Person.prin() //静态方法调用