What Is The Main Core Difference Between A Javascript Function And Javascript Object?
Solution 1:
When you do a "typeof" on both of these types you get "function" for functions and "object" for objects,
First of all, the specification for typeof
is basically just a lookup table, where it says "if the value is a function object, return the string "function"
). So it doesn't provide the real data type of the value (which would be object for functions).
but isn't it so that functions are special objects
Yes. Functions are so called callable objects.
Objects have, in addition to "normal" properties, so called "internal" properties. You can think of these as some kind of internal state, that needs to be maintained for the object to work correctly, but that is not accessible in user code.
Some of these internal properties make certain objects special because not every object has them. One of them is [[Call]]
(internal properties are denoted with double brackets), which contains code in some implementation-specific format.
When you call a value (i.e. foo()
) the interpreter first checks whether the value is an object and then checks whether it has an intern [[Call]]
property. If yes, then the code stored in that property is executed.
Here is a very rough example of how this could look like internally:
// Simulates a function call implementation, i.e. what happens when// you do `foo()`functioncall(value) {
if (typeof value !== "object") {
thrownewError('Not an object');
}
if (!value["[[Call]]"]) {
thrownewError('Not a function');
}
returneval(value["[[Call]]"]);
}
// Simulated function object that has a name ("normal" property)// and the internal property [[Call]].// This would be the internal representation for something like// function func() {// console.log('some code');// }var func = {
name: "func",
"[[Call]]": "console.log('I\\'m a function!');",
};
call(func);
Side note: If you know Python, then this concept should be familiar to you, because Python allows you to make arbitrary objects callable by implementing __call__
.
In addition to [[Call]]
there is also [[Construct]]
. We actually distinguish between callable and constructable functions. Constructable functions are those that can be invoked with new
. Functions created via function ...
are both callable and constructable. Arrow functions are only callable, functions created via class ...
are only constructable. And this distinction is made based on whether [[Call]]
or [[Construct]]
or both are set.
if so what are the properties that differentiate a function from an object
In addition to these special internal properties, function objects also have all properties defined on Function.prototype
:
console.dir(Function.prototype);
// etc
(and Function.prototype
"extends" Object.prototype
) which is where .call
and .apply
are defined, but these alone do not make functions special. The internal [[Call]]
property is what makes them special.
The same applies to other built-in "classes" such as Array
, Date
or RegExp
. All instances of these classes have additional methods/properties that are defined on Array.prototype
, Date.prototype
, etc.
Solution 2:
Functions (which are first-class objects) can be called, while other objects cannot.
See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions for more detail.
Post a Comment for "What Is The Main Core Difference Between A Javascript Function And Javascript Object?"