Please, explain to me why functions are objects in javascript? An object is a structure with properties. Value of property may be a function and we call this property - method. But we can NOT execute an object. We can NOT do this:
var cat = {name: 'Murzik', age: 17};
cat();
But we can execute the function
var func = function() {
alert('Hello world!');
};
func();
So, if functions are objects why we can do this? Please, help me to understand! Thank's!
4 Answers 4
Because the ECMAScript spec says so:
member of the Object type that is an instance of the standard built-in
Functionconstructor and that may be invoked as a subroutine
Note that
Some objects are not callable:
var obj = {}; typeof obj; // "object" --> It's not callable obj(); // TypeError: obj is not a functionSome objects are callable but are not functions:
var obj = document.createElement('object'); typeof obj; // "function" --> It's callable obj instanceof Function; // false --> It's not a functionSome objects are callable and are functions:
function obj(){} typeof obj; // "function" --> It's callable obj instanceof Function; // true --> It's a Function instanceNot all
Functioninstances are callable:var obj = Object.create(Function.prototype); obj instanceof Function; // true --> It's a Function instance typeof obj; // "object" --> It's not callable obj(); // TypeError: obj is not a function
Comments
It's just the way the language is designed. Functions are reference types (they are passed as pointers), and JavaScript reference types may have sub-references. I think you are struggling to reconcile a model of objects from C++ or Java (structs with privileged methods) with the type sugar another language provides.
I am not sure at what point Brendan Eich chose to implement functions as an instance of objects, but I suspect the reasoning was that it was consistent with making functions first-class variables. Objects already existed as a type of variable and it perhaps made sense to model functions using an existing variable type. You would have to ask him directly.
One handy side effect is that it allows us to store metadata on a function. This can be handy when performing caching or doing anything else that requires state to be held between function calls:
function myCachedFunction(argument) {
myCachedFunction.cache = myCachedFunction.cache || {};
myCachedFunction.cache[argument] = myCachedFunction.cache[argument] || operation();
return myCachedFunction.cache[argument];
function operation() {
// performs action with argument
// only gets called if no cache lookup for argument
}
}
Some functions also need to know when they've been called. This can be achieved by closure variables, but function fields can sometimes be a more elegant solution:
function getUniqueID() {
getUniqueID._calls = getUniqueID._calls || 0;
return getUniqueID._calls++;
}
2 Comments
apply and toString would not even necessitate any special cases when functions were primitive values. Methods work for other primitive types just as wellSimply because they are... thats what the designers of the language decided to do.
Comments
What would you expect to happen if an object were callable? You can think of an object as storage and a function as something you can execute. Also, functions are not objects but they are a superset of them. Due to this libraries like jquery are possible as is prototypical inheritance which is how you make a class in JavaScript.
function Car() {
this.tires = 4;
}
Car.prototype.hi = function() {
console.log("hi");
}
var myCar = new Car();
console.log(myCar.tires); // 4
myCar.hi(); // hi
And static methods
Car.bye = function() {
console.log("bye");
}
Car.bye(); // bye
myCar.bye(); // this is an error
3 Comments
var obj = { method: function() { alert(1) } }; obj.method(); But how we can call the function? What method we are calling, when we call the function, if we think that an object is a storage with properties.
is ais not equivalence.