18

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

salmane
  • 4,799
  • 13
  • 48
  • 61
  • http://www.packtpub.com/article/using-prototype-property-in-javascript – jjj Jan 24 '10 at 11:39
  • 1
    Extending build-in JavaScript objects is bad practice. – jerone Jan 24 '10 at 12:14
  • As to why extending built-in objects or prototypes is bad, imagine a piece of JS code does it, then someone forks that code and modifies that behavior, and somewhere down the line someone runs the original and forked code on the same page. Or, someone else just happens to extend a built-in object or prototype using the same name but in a different way. There is no isolation or guarantees that code won't conflict. – thomasrutter Dec 22 '21 at 21:58

4 Answers4

32

Sure. Functions are objects:

var foo = function() {};

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

foo.bar();

Will alert "bar"

troelskn
  • 115,121
  • 27
  • 131
  • 155
  • This code snippet assumes that `foo` has been declared previously, or else `foo` is an implied global (bad!). Perhaps change the declaration statement to `var foo = function() {};` to reinforce good practice...? – Steve Harrison Apr 18 '10 at 07:19
  • Well, it's not exactly good style to modify the prototype of base objects in the first place. But I concur. – troelskn Apr 18 '10 at 16:26
3

Try Object.prototype instead:

Object.prototype.methodName = function(){ 

  return dowhateverto(this) 
};

But also heed the warning that extending native objects is not always a good idea.

Function.prototype can't really be manipulated reliably in Javascript; it isn't a real object, because Function() constructor needs to return a function, not an object. But, you can't treat it like a normal function either. Its behaviour when you try to access its 'properties' may be undefined and vary between browsers. See also this question.

Community
  • 1
  • 1
thomasrutter
  • 114,488
  • 30
  • 148
  • 167
1
function one(){
    alert(1);
}
function two(){
    alert(2);
}

var myFunctionNames = ["one", "two"];

for(var i=0; i<myFunctionNames.length; i++) {
    // reference the window object, 
    // since the functions 'one' and 'two are in global scope
    Function.prototype[myFunctionNames[i]] = window[myFunctionNames[i]];
}


function foo(){}

foo.two(); // will alert 2
Luca Matteis
  • 29,161
  • 19
  • 114
  • 169
0

Changing JS built-in objects can give you some surprises.
If you add external libraries or changing the version of one of them, you are never sure that they won't overwrite your extension.

Mic
  • 24,812
  • 9
  • 57
  • 70