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.

shiv garg
  • 761
  • 1
  • 8
  • 26
  • 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()` – dandavis Mar 03 '16 at 04: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. – dandavis Mar 03 '16 at 04:53
  • so can i say that when i call function e.g console.log(rabbit()) i am calling some specific property of that object – shiv garg Mar 03 '16 at 04: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) – dandavis Mar 03 '16 at 04: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. – Tony Dinh Apr 25 '16 at 17:51
  • Does this answer your question? [A function and an object at the same time?](https://stackoverflow.com/questions/33097986/a-function-and-an-object-at-the-same-time) – Sebastian Simon Apr 11 '21 at 17:28
  • yes, it does answer my question. – shiv garg Apr 18 '21 at 04:15

2 Answers2

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.
Tony Dinh
  • 6,668
  • 5
  • 39
  • 58
  • I don't see the purpose of showing the `new Function` case. –  Mar 03 '16 at 05:05
  • 1
    @torazaburo just try to emphasize that `Rabbit` is just an object created by `new Function`. – Tony Dinh Mar 03 '16 at 05:09
  • I edited the example to make it more semantic related to the question. – Tony Dinh Mar 03 '16 at 05:11
  • 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 :1:3 – Srijith Ramachandran Dec 20 '19 at 12:47
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.

gurvinder372
  • 66,980
  • 10
  • 72
  • 94