2

If I check the type of the Object object it says "function":

typeof Object === "function"

But we all know, that Object has multiple methods such as:

Object.create();
Object.freeze();
Object.seal();
Object.getPrototypeOf();
...

(check https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)

My question: how is that possible? How a function is able to have methods? I always thought, that a method is a function that is the value of a property of an object.

Here we use it as an object:

Object.freeze();

and here we use it as a constructor function:

var myObj  = new Object();
var myObj2 = Object();

What is "Object" now? It seems it is both: function and object.

Is it a special case because these (Object, Array, String, Number) are the "native constructors"?

Teemoh
  • 332
  • 2
  • 11

5 Answers5

4

A Function is a an object in Javascript and thus can both be called like a function AND can have properties (e.g. methods).

function f() {
    console.log("hello");
}

f.greeting = "goodbye";

f();                       // outputs "hello"
console.log(f.greeting);   // outputs "goodbye"

Note that functions also have built-in properties and methods such as .call(), .apply() and .length, .prototype, etc... See the description of the Function object on MDN for more info on the built in properties/methods.

Object itself is a constructor function that is meant to be used like this:

var x = new Object();

Though normally one will use the object literal syntax to declare a new object:

var x = {};

As a constructor function Object can both be called as a constructor (using new) and it can have properties/methods of its own.

My question: how is that possible? How a function is able to have methods? I always thought, that a method is a function that is the value of a property of an object.

This is how Javascript is designed. Functions are objects and can have properties/methods of their own.

Is it a special case because these (Object, Array, String, Number) are the "native constructors"?

This is not a special case. This is true for all functions. As my code example above shows, you can define your own functions and give them properties/methods.

jfriend00
  • 683,504
  • 96
  • 985
  • 979
  • Ok alright, I understood! Thanks for your answer. But why for example "join" is defined in the function itself and the protoype tree (Array.join vs Array.prototype.join)? – Teemoh Nov 14 '15 at 22:13
  • 1
    @souser - things on the prototype are methods of **instances** of an object. Things on the constructor function are static methods that are called without an instance. So, `Array.isArray()` is a static method, but `Array.prototype.concat()` is a method of an instance of an array. Do you understand the difference between static methods and instance methods? – jfriend00 Nov 14 '15 at 22:14
  • To be more precise, if you assign thing to prototype it is not on an "instance" of that type but when JavaScript is looking for that method it goes up the prototype chain till it find one. It means that on instance itself is nothing defined until you explicitly do "onstance.myFunction = sth;" – svobol13 Nov 14 '15 at 22:19
  • Yes, I understand the difference. But still dont understand why we need one function defined in two places. But I already found some already asked questions for this: http://stackoverflow.com/questions/10767129/why-cant-i-use-array-join-call-in-place-of-array-prototype-join-call http://stackoverflow.com/questions/12771356/in-javascript-is-array-join-really-array-prototype-join – Teemoh Nov 14 '15 at 22:20
  • @souser - your specific question about `Array.join()` confused me because that is NOT part of the ECMAScript standard and is not implemented in Chrome or Edge. It appears to be a Firefox non-standard thing. Try this in different browsers: http://jsfiddle.net/jfriend00/91zz4dvk/ – jfriend00 Nov 14 '15 at 22:27
  • Yeah true, only firefox does this. I didnt check it in another browser before. So you can forget my comment above. – Teemoh Nov 14 '15 at 22:30
2

A function is a number is a string is a boolean is an array is an object.

Okay, well, it's not that simple. But a function is an object and can have properties just like every other object in JavaScript:

var a = function() { return 10; };
a.foo = function() { return 20; };
console.log(a());     // 10
console.log(a.foo()); // 20
tckmn
  • 57,719
  • 27
  • 114
  • 156
1

An instance of a function is an object just like any other - it's free to have methods defined on it.

Rich O'Kelly
  • 41,274
  • 9
  • 83
  • 114
  • Ah ok, I never thought about adding a property to an function, but good to know that it is possible! – Teemoh Nov 14 '15 at 21:55
1

how is that possible?

In JavaScript, Functions are first class objects. So as a function object can have properties as well as methods.

var a = new Object()?

What is "Object" now?

Here Object acts as a Constructor function, which will create a new object.

Community
  • 1
  • 1
rajuGT
  • 6,224
  • 2
  • 26
  • 44
1

Object is "constructor" function. It is not the same as var obj = {};. Typeof Object is function, typeof obj is object.

In Javascript is everything "object" and can have "properties" and "methods".

Try this example:

function Class() {
    if (this === window) { 
        return new Class();
    }
}; 
// You create new 'type' Class (it's same with Object)
// You can invoke it (in browser) as function like Class();
// Or as a constructor like new Class();
// Or treat is as object - you can add properties
Class.getInstance = function() { return new Class(); }; // Here you add 'property' and its value is function
typeof Class === typeof Object; // true
Class.getInstance(); // You you function that calls your 'type' as 'constructor' (with new operator).

And you are absolutely right. Function is special case of Object so function is both at same time. Its like Dog is Dog and Animal in same time.

Ad last point: In JavaScript is everything object and some object are special - they can by "invoked"/"called". Those are functions (they are "first-class-citizens"). Array, Number,... are "just" native-constructor-functions.

svobol13
  • 1,842
  • 3
  • 25
  • 40