javascript add prototype method to all functions?

前端 未结 4 876
星月不相逢
星月不相逢 2021-02-05 09:54

Is there a way to add a method to all javascript functions without using the prototype library?

something along the lines of :

Function.prototype.methodN         


        
4条回答
  •  被撕碎了的回忆
    2021-02-05 10:40

    Sure. Functions are objects:

    var foo = function() {};
    
    Function.prototype.bar = function() {
      alert("bar");
    };
    
    foo.bar();
    

    Will alert "bar"

提交回复
热议问题