javascript为所有函数添加原型方法? [英] javascript add prototype method to all functions?

查看:128
本文介绍了javascript为所有函数添加原型方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法在不使用原型库的情况下为所有javascript函数添加方法?



类似于以下内容:

  Function.prototype.methodName = function(){


return dowhateverto(this)

};

这是我到目前为止所尝试的但它没有用。也许这也是一个坏主意,如果可以,请告诉我为什么?



如果可以,我可以将它添加到我选择的一组函数中



类似于:

  MyFunctions.prototype.methodName = function(){


返回dowhateverto(this)

};

其中MyFunctions是一个函数名数组



<谢谢

解决方案

当然。函数是对象:

  var foo = function(){}; 

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

foo.bar();

将提醒bar


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

something along the lines of :

Function.prototype.methodName = function(){ 


  return dowhateverto(this) 

 };

this is what i tried so far but it didnt work. Perhaps it is a bad idea also if so could you please tell me why?

if so can I add it to a set of functions i choose

something like :

MyFunctions.prototype.methodName = function(){ 


  return dowhateverto(this) 

 };

where MyFunctions is an array of function names

thank you

解决方案

Sure. Functions are objects:

var foo = function() {};

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

foo.bar();

Will alert "bar"

这篇关于javascript为所有函数添加原型方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆