javascript adding property to function

Function in JavaScript is just an object, it is called Function object.

And just like any other types of object, it has its own constructor (new Function(...)), methods (apply, bind, call…) and properties (arguments, caller, name…) . See the document.

You might be familiar with creating a function like this:

function Rabbit() {
    console.log('shiv');
}

Then you should know that you can also create a function like this:

var Rabbit = new Function('console.log("shiv")');

Now, you might guess it out. If you add a new property to a Function object, as long as you don’t overwrite the existing one, the function is still working just fine.

do this add a variable bark to function

  • No, the function has it own closure, the only way to add variable to the function is to bind it to this object using Rabbit.bind(object)

do this added a property to Rabbit object

  • Well, since the “Rabbit object” is just an object, Yes.

Leave a Comment