mx_code
mx_code

Reputation: 2517

How to add methods or props to a functions?

How to add methods or props to a functions?

Following is a function:


function testFunction()  {
  console.log('test')
}

Like this: enter image description here

So you can see a lot of methods like apply, bind,call and properties like arguments, caller,length etc...

How can I add a static method or property to my function like this:

testFunction.someMethod() {

}

Such that it will be available in the above mentioned list.

I;m not talking about function.prototype. I know it is possible. I just wanted to know if custom static methods (like bind,call or apply) are creatable such that it will be avaialable in the above mentioned list.

Upvotes: 0

Views: 154

Answers (1)

BrunoLM
BrunoLM

Reputation: 100331

function testFunction()  {
  console.log('test')
}
testFunction.someMethod = function() {
  console.log('some method');
}

testFunction.someMethod();

code

This is not a common thing to do. I'd say it's better to use a class instead or take a different approach.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes

Upvotes: 2

Related Questions