定义在构造函数内的方法就是实例方法
- function A(name){
- this.name = name
- this.eat = function(){
- console.log(this.name + "吃龙虾")
- }
- }
- var a = new A("逍遥的码农")
- a.eat()
构造函数 = function 的是静态方法
- function A(name){
- this.name = name
- this.eat = function(){
- console.log(this.name + "吃龙虾")
- }
- }
- A.say = function(){
- console.log("我是静态方法")
- }
- A.say()
ts中定义实例方法函数前不加static关键字
- class A {
- name: string;
- constructor(name: string) {
- this.name = name;
- }
- eat(){
- console.log(this.name + "吃零食")
- }
- }
- var a = new A("逍遥的码农")
- a.eat()
静态方法就是给函数前加上static关键字
- class A{
- name:string
- constructor(name:string){
- this.name = name
- }
- static eat(){
- console.log("我是静态方法")
- }
- }
- var a = new A("逍遥的码农")
- a.eat() // 会提示属性“eat”在类型“A”上不存在。你的意思是改为访问静态成员“A.eat”吗?