Reputation: 342
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"?
Upvotes: 2
Views: 633
Reputation: 1950
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.
Upvotes: 1
Reputation: 6404
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.
Upvotes: 1
Reputation: 707318
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.
Upvotes: 4
Reputation: 41757
An instance of a function is an object just like any other - it's free to have methods defined on it.
Upvotes: 1
Reputation: 59273
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
Upvotes: 2