js 静态方法和实例方法

function F() {
Object.prototype.a = function () {
console.log("a()");
};
Function.prototype.b = function () {
console.log("b()");
};
}

var f = new F();
typeof f
// 'object'

typeof F
//'function'

f.a

F.b

function C(){
this.a=function(res){console.log(res)}
}

js的静态方法实例方法


 静态方法
 function Test(){}
 Test.to=function(res){console.log(res)}
 Test.to('32131') //32131

 静态方法可以通过函数名或者类名直接调用


 实例方法
 function Cat(name){
   this.name = name
   this.move = function() {
     console.log('移动')
   }
   this.eat = function() {
     console.log(`${this.name}爱吃鱼`)
   }
 }
 //给Cat构造函数添加静态方法
 Cat.eat = function() {console.log(`${this.name}爱吃鱼`)}
 let cat = new Cat('tom')
 Cat.eat()  //Cat爱吃鱼  //这是静态方法
 Cat.move() //Cat.move is not a function
 cat.eat()  //tom爱吃鱼  //这是实例方法
 cat.move()  //移动     //这是实例方法
 

 






發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章