16

Let us say we have a function.

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

Now without creating an object of this function i can assign the property of this object

Rabbit.bark = function(line) {
 console.log("name is", line);
};

What does this mean. do this add a variable bark to function. or does this add a property to Rabbit object, even if I am not creating an object using the new operator.

7
  • 5
    functions are objects, bark is a property of an object. bark is not a traditional method however, but if you added new and prototype into the mix, then bark would be a method of instances. as-is, there's no connection from .bark to any instance if you simply added new to the code above, but you can reach bark as a static method, like Array.isArray() Commented Mar 3, 2016 at 4:48
  • 4
    think of a function as an object, something like {name: "Rabbit", length: 0, body: "code here", arguments: [], this: {}, _closures: {}, call: function call()... } you can add new properties yourself if you'd like. Commented Mar 3, 2016 at 4:53
  • so can i say that when i call function e.g console.log(rabbit()) i am calling some specific property of that object Commented Mar 3, 2016 at 4:55
  • 3
    yeah, basically the function's .call() method is how you invoke it. close enough. to be nerdy, there are internal hidden properties inside functions you can read about in the spec that you are actually calling/using, not a userland endpoint (instance method) Commented Mar 3, 2016 at 4:57
  • Hi @shivgarg, if you think my answer helped, you can mark it as the accepted answer which helps others to quickly find it. Thanks. Commented Apr 25, 2016 at 17:51

2 Answers 2

7

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.
Sign up to request clarification or add additional context in comments.

4 Comments

I don't see the purpose of showing the new Function case.
@torazaburo just try to emphasize that Rabbit is just an object created by new Function.
I edited the example to make it more semantic related to the question.
if i create a instance of Rabbit, i am not able to access the property "bark". whats the issue..can someone explain? function Rabbit(){ console.log("shiv"); } Rabbit.bark = function(line) { console.log("name is", line); }; var m = new Rabbit(); m.bark("test"); VM135:1 Uncaught TypeError: m.bark is not a function at <anonymous>:1:3
3

what does this mean. do this add a variable bark to function. or do this added a property to Rabbit object

do this add a variable bark to function - No

or do this added a property to Rabbit object - Yes

bark is a property of object of type Function

even if i am not creating an object using new operator

Rabit is already an object (of type Function). You are not creating an instance of this object, just that you are adding a property to it.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.