Is `Object` a function in javascript?__proto__ VS. prototype in JavaScriptHow do JavaScript closures...
What would be some possible ways of escaping higher gravity planets?
How do I narratively explain how in-game circumstances do not mechanically allow a PC to instantly kill an NPC?
Where does documentation like business and software requirement spec docs fit in an agile project?
What does an unprocessed RAW file look like?
How can I take a waterfall's effect on a jump into consideration mechanically?
Is it possible to rotate the Isolines on a Surface Using `MeshFunction`?
I have trouble understanding this fallacy: "If A, then B. Therefore if not-B, then not-A."
What's the reason that we have a different number of days each month?
Why does alert(0.-5) print -5?
Is it really OK to use "because of"?
How to deal with an underperforming subordinate?
How can I differentiate duration vs starting time
RS485 using USART or UART port on STM32
Is the percentage symbol a constant?
Rigorous justification for non-relativistic QM perturbation theory assumptions?
Does it take energy to move something in a circle?
Taking an academic pseudonym?
Why did Ylvis use "go" instead of "say" in phrases like "Dog goes 'woof'"?
What is a good way to explain how a character can produce flames from their body?
How to put text above column in minipage?
Why is "rm -r" unable to delete this folder?
Was there a pre-determined arrangement for the division of Germany in case it surrendered before any Soviet forces entered its territory?
How to align the top of the text with the top of a figure produced by tikz in minipage
The No-Straight Maze
Is `Object` a function in javascript?
__proto__ VS. prototype in JavaScriptHow do JavaScript closures work?What is the most efficient way to deep clone an object in JavaScript?How do I remove a property from a JavaScript object?var functionName = function() {} vs function functionName() {}Which equals operator (== vs ===) should be used in JavaScript comparisons?How do I include a JavaScript file in another JavaScript file?What does “use strict” do in JavaScript, and what is the reasoning behind it?How to check whether a string contains a substring in JavaScript?How do I remove a particular element from an array in JavaScript?__proto__ VS. prototype in JavaScript
Consider this function:
function Foo(){
var a = "3";
};
According to __proto__ VS. prototype in JavaScript,
Foo.__proto__ = Function.prototye
Function.prototype.__proto__ = Object.prototype
I understood that part, but if i do this in chrome console:
Object.__proto__
output: ƒ () { [native code] }
Function.__proto__
output: ƒ () { [native code] }
Q1: why are they pointing to Function? what actually are Function and Object and how are they different from each other, because Object is actually a function:
typeof Object
"function"
Q2: If everything is an object in JS, then why is Object
a function? also how is a function actually implemented inside JS? what happens to the variables declared inside a function? is a function converted into an object by JS compiler?
sorry if i am missing something obvious, i am really confused by the way function and object are implemented in JS
javascript prototype
|
show 10 more comments
Consider this function:
function Foo(){
var a = "3";
};
According to __proto__ VS. prototype in JavaScript,
Foo.__proto__ = Function.prototye
Function.prototype.__proto__ = Object.prototype
I understood that part, but if i do this in chrome console:
Object.__proto__
output: ƒ () { [native code] }
Function.__proto__
output: ƒ () { [native code] }
Q1: why are they pointing to Function? what actually are Function and Object and how are they different from each other, because Object is actually a function:
typeof Object
"function"
Q2: If everything is an object in JS, then why is Object
a function? also how is a function actually implemented inside JS? what happens to the variables declared inside a function? is a function converted into an object by JS compiler?
sorry if i am missing something obvious, i am really confused by the way function and object are implemented in JS
javascript prototype
"typeof Object" Of course, it is a function because this is constructor function. EvenNumber
, orBoolean
are functions.
– dfsq
1 hour ago
Do note that there is a difference between "object" (the concept) and the function "Object". In JavaScript, all functions are objects, including "Object". On the other hand not all objects are functions (eg.var a = {}
)
– TiiJ7
54 mins ago
@TiiJ7 "In JavaScript, all functions are objects, including "Object" , how exactly? we can use dot operator with an object but not with a function to access it's local variables
– vikrant
41 mins ago
You are doing exactly that, though:Object.__proto__
=> You get the property__proto__
fromObject
, which is afunction
and thus anobject
.
– TiiJ7
39 mins ago
1
Just to make it sure, see another fiddle = ).
– Teemu
4 mins ago
|
show 10 more comments
Consider this function:
function Foo(){
var a = "3";
};
According to __proto__ VS. prototype in JavaScript,
Foo.__proto__ = Function.prototye
Function.prototype.__proto__ = Object.prototype
I understood that part, but if i do this in chrome console:
Object.__proto__
output: ƒ () { [native code] }
Function.__proto__
output: ƒ () { [native code] }
Q1: why are they pointing to Function? what actually are Function and Object and how are they different from each other, because Object is actually a function:
typeof Object
"function"
Q2: If everything is an object in JS, then why is Object
a function? also how is a function actually implemented inside JS? what happens to the variables declared inside a function? is a function converted into an object by JS compiler?
sorry if i am missing something obvious, i am really confused by the way function and object are implemented in JS
javascript prototype
Consider this function:
function Foo(){
var a = "3";
};
According to __proto__ VS. prototype in JavaScript,
Foo.__proto__ = Function.prototye
Function.prototype.__proto__ = Object.prototype
I understood that part, but if i do this in chrome console:
Object.__proto__
output: ƒ () { [native code] }
Function.__proto__
output: ƒ () { [native code] }
Q1: why are they pointing to Function? what actually are Function and Object and how are they different from each other, because Object is actually a function:
typeof Object
"function"
Q2: If everything is an object in JS, then why is Object
a function? also how is a function actually implemented inside JS? what happens to the variables declared inside a function? is a function converted into an object by JS compiler?
sorry if i am missing something obvious, i am really confused by the way function and object are implemented in JS
javascript prototype
javascript prototype
edited 29 mins ago
vikrant
asked 1 hour ago
vikrantvikrant
508614
508614
"typeof Object" Of course, it is a function because this is constructor function. EvenNumber
, orBoolean
are functions.
– dfsq
1 hour ago
Do note that there is a difference between "object" (the concept) and the function "Object". In JavaScript, all functions are objects, including "Object". On the other hand not all objects are functions (eg.var a = {}
)
– TiiJ7
54 mins ago
@TiiJ7 "In JavaScript, all functions are objects, including "Object" , how exactly? we can use dot operator with an object but not with a function to access it's local variables
– vikrant
41 mins ago
You are doing exactly that, though:Object.__proto__
=> You get the property__proto__
fromObject
, which is afunction
and thus anobject
.
– TiiJ7
39 mins ago
1
Just to make it sure, see another fiddle = ).
– Teemu
4 mins ago
|
show 10 more comments
"typeof Object" Of course, it is a function because this is constructor function. EvenNumber
, orBoolean
are functions.
– dfsq
1 hour ago
Do note that there is a difference between "object" (the concept) and the function "Object". In JavaScript, all functions are objects, including "Object". On the other hand not all objects are functions (eg.var a = {}
)
– TiiJ7
54 mins ago
@TiiJ7 "In JavaScript, all functions are objects, including "Object" , how exactly? we can use dot operator with an object but not with a function to access it's local variables
– vikrant
41 mins ago
You are doing exactly that, though:Object.__proto__
=> You get the property__proto__
fromObject
, which is afunction
and thus anobject
.
– TiiJ7
39 mins ago
1
Just to make it sure, see another fiddle = ).
– Teemu
4 mins ago
"typeof Object" Of course, it is a function because this is constructor function. Even
Number
, or Boolean
are functions.– dfsq
1 hour ago
"typeof Object" Of course, it is a function because this is constructor function. Even
Number
, or Boolean
are functions.– dfsq
1 hour ago
Do note that there is a difference between "object" (the concept) and the function "Object". In JavaScript, all functions are objects, including "Object". On the other hand not all objects are functions (eg.
var a = {}
)– TiiJ7
54 mins ago
Do note that there is a difference between "object" (the concept) and the function "Object". In JavaScript, all functions are objects, including "Object". On the other hand not all objects are functions (eg.
var a = {}
)– TiiJ7
54 mins ago
@TiiJ7 "In JavaScript, all functions are objects, including "Object" , how exactly? we can use dot operator with an object but not with a function to access it's local variables
– vikrant
41 mins ago
@TiiJ7 "In JavaScript, all functions are objects, including "Object" , how exactly? we can use dot operator with an object but not with a function to access it's local variables
– vikrant
41 mins ago
You are doing exactly that, though:
Object.__proto__
=> You get the property __proto__
from Object
, which is a function
and thus an object
.– TiiJ7
39 mins ago
You are doing exactly that, though:
Object.__proto__
=> You get the property __proto__
from Object
, which is a function
and thus an object
.– TiiJ7
39 mins ago
1
1
Just to make it sure, see another fiddle = ).
– Teemu
4 mins ago
Just to make it sure, see another fiddle = ).
– Teemu
4 mins ago
|
show 10 more comments
5 Answers
5
active
oldest
votes
Unlike other languages, JavaScript considers "function" as an object:
typeof Object // "function"
class Test {}
typeof Test // "function"
But null returns a "object":
typeof null // "object" => bad
If you need to consider intrinsic "object", you'll need to get it from prototype
:
typeof Object.prototype // "object"
typof Object.__proto__ // "function"
__proto__
is an internal function. But prototype
is a mechanism to get its parent definitions. So, using __proto__
will return the type as a function
while prototype
will return object
checking its definition.
So, calling Object.prototype
is nothing but it's just getting the name from it's constructor Object.prototype.constructor.name
.
So, if you do deeper drive, then you'll get "object" after following __proto__
:
typeof Object.__proto__.__proto__.__proto__ // "object"
At this point, you're at getting an object by checking it's definition. That's it. - Though, I am not able to clear up things to you.
Let me re-visit your question:
When you call typeof Object
, the JS will run its __proto__
.
So,
typeof Object
// evaluates:
typeof Object.__proto__ // "function"
typeof Object.__proto__.__proto__
// evaluates:
typeof Object.__proto__.__proto__.__proto__ // "object"
So when you call its prototype:
typeof Object.prototype
// evaluates:
typeof Object.prototype.__proto__ // "object"
This should satisfy your question 2 as well. Hope, you understand now.
And the following should throw you an error:
typeof Object.prototype.__proto__.__proto__ // TypeError
Because, at the end of its prototype chain, there's null
which is considered as object in JS but it cannot read the property __proto__
of null
.
but it is displaying a function as a "function" typeof(Foo) output: "function"
– vikrant
1 hour ago
Yes, function is as well considered object but is intrinsically a function.
– Bhojendra Rauniyar
1 hour ago
1
so everything is a function behind the scenes, because everywhere it is mentioned that in JS everything is an Object. "function is as well considered object but is intrinsically a function", i am not able to understand that part sorry
– vikrant
1 hour ago
Just remember function is nothing but an object.
– Bhojendra Rauniyar
47 mins ago
but how exactly? :) i don't understand what do you mean by "considers"? if it is a function, then it is a function, why is it considered as an object, is it converted to an object by js engine?
– vikrant
45 mins ago
|
show 3 more comments
Q1: why are they pointing to Function?
A1: Because they are functions. Function
and Object
are just constructor functions.
Q2: If everything is an object in JS, then why is Object a function?
A2: Because Object
is just a constructor function.
typeof Object
// 'function'
typeof new Object()
// 'object'
And a function is a instance of Function
, so that makes a function object.
(function(){}).constructor === Function
// true
"And a function is a instance of Function, so that makes a function object." why does that make a function object?
– vikrant
40 mins ago
What I was trying to say is just,Function
constructs an object which is function.
– zmag
32 mins ago
if it is a function, why is it called object?
– vikrant
28 mins ago
because it's an object ofFunction
. difference betweenFunction
andfunction
is a point.
– zmag
26 mins ago
add a comment |
Object
is the constructor function of all objects. So, typeof Object==="function"
Here is a snippet for visualisation:
console.log(typeof Object) //function (which is an object)
var object=new Object() //An instance of Object 'class'
console.log(typeof object) //object
console.log(object instanceof Object) //true, because object is created by Object()
Function
is the constructor function of all functions (including itself...)
So, although not everything is a function, but most of Uppercase native identifiers refers to a constructor function.
add a comment |
Fundamentally
Functions
has some code that can be executed.
Object
are those which contains data.
For class Point
having x
and y
.
class Point {
constructor(x, y) {
this.x = x;
this.y = y;
}
isOrigin() { return x == 0 && y == 0; }
}
let p = new Point();
Answer 1
in this p
is object
which contains data or other functions.
p.isOrigin
is function.
Similarly class Point
is itself a function
which when runs produces p
. Thats why all classes like Object
and Functions
are functions
more precisely constructor functions
.
Answer2
If everything is an object in JS, then why is Object a function?
Same as Answer1.
Also how is a function actually implemented inside JS?
Different js engine will have different implementations. They have specifications which they need to follow.
What happens to the variables declared inside a function?
Off topic. Every function
runs with a scope which holds all data for that functions.
Is a function converted into an object by JS compiler?
Not sure what are you asking.
add a comment |
Function and Object are both constructor function which can be used to create a function and an object respectively which is the reason typeof Function
returns function
About the similarities and differences between and object and a function, consider the following points:
- All non primitive types are objects in javascript.
- All objects indirectly inherit from Object.prototype which inherits from null.
- All native functions inherit from Function.prototype which inherits from Object.prototype so it means function indirectly inherits from Object.prototype.
- A function can be called using
()
operator because javascript engine knows it is declared using a function keyword and it inherits from Function.prototype and have executable code, so whenever it is called, javascript engine creates a new execution context and set thethis
binding and then executes the function. None of that happens when you try to call an object instead an error is thrown i.e. "is not a function". So we can say that not every object is a function because not all objects inherit from Function.prototype but all objects do inherit from Object.prototype. - As function indirectly inherits from Object.prototype, you can treat it as an object e.g. add properties to it, create new objects from it.
- Because a function indirectly inherits from Object.prototype, we call it an object but it is different from other objects because it inherits directly from Function.prototype and has code that can be executed.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54861385%2fis-object-a-function-in-javascript%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
5 Answers
5
active
oldest
votes
5 Answers
5
active
oldest
votes
active
oldest
votes
active
oldest
votes
Unlike other languages, JavaScript considers "function" as an object:
typeof Object // "function"
class Test {}
typeof Test // "function"
But null returns a "object":
typeof null // "object" => bad
If you need to consider intrinsic "object", you'll need to get it from prototype
:
typeof Object.prototype // "object"
typof Object.__proto__ // "function"
__proto__
is an internal function. But prototype
is a mechanism to get its parent definitions. So, using __proto__
will return the type as a function
while prototype
will return object
checking its definition.
So, calling Object.prototype
is nothing but it's just getting the name from it's constructor Object.prototype.constructor.name
.
So, if you do deeper drive, then you'll get "object" after following __proto__
:
typeof Object.__proto__.__proto__.__proto__ // "object"
At this point, you're at getting an object by checking it's definition. That's it. - Though, I am not able to clear up things to you.
Let me re-visit your question:
When you call typeof Object
, the JS will run its __proto__
.
So,
typeof Object
// evaluates:
typeof Object.__proto__ // "function"
typeof Object.__proto__.__proto__
// evaluates:
typeof Object.__proto__.__proto__.__proto__ // "object"
So when you call its prototype:
typeof Object.prototype
// evaluates:
typeof Object.prototype.__proto__ // "object"
This should satisfy your question 2 as well. Hope, you understand now.
And the following should throw you an error:
typeof Object.prototype.__proto__.__proto__ // TypeError
Because, at the end of its prototype chain, there's null
which is considered as object in JS but it cannot read the property __proto__
of null
.
but it is displaying a function as a "function" typeof(Foo) output: "function"
– vikrant
1 hour ago
Yes, function is as well considered object but is intrinsically a function.
– Bhojendra Rauniyar
1 hour ago
1
so everything is a function behind the scenes, because everywhere it is mentioned that in JS everything is an Object. "function is as well considered object but is intrinsically a function", i am not able to understand that part sorry
– vikrant
1 hour ago
Just remember function is nothing but an object.
– Bhojendra Rauniyar
47 mins ago
but how exactly? :) i don't understand what do you mean by "considers"? if it is a function, then it is a function, why is it considered as an object, is it converted to an object by js engine?
– vikrant
45 mins ago
|
show 3 more comments
Unlike other languages, JavaScript considers "function" as an object:
typeof Object // "function"
class Test {}
typeof Test // "function"
But null returns a "object":
typeof null // "object" => bad
If you need to consider intrinsic "object", you'll need to get it from prototype
:
typeof Object.prototype // "object"
typof Object.__proto__ // "function"
__proto__
is an internal function. But prototype
is a mechanism to get its parent definitions. So, using __proto__
will return the type as a function
while prototype
will return object
checking its definition.
So, calling Object.prototype
is nothing but it's just getting the name from it's constructor Object.prototype.constructor.name
.
So, if you do deeper drive, then you'll get "object" after following __proto__
:
typeof Object.__proto__.__proto__.__proto__ // "object"
At this point, you're at getting an object by checking it's definition. That's it. - Though, I am not able to clear up things to you.
Let me re-visit your question:
When you call typeof Object
, the JS will run its __proto__
.
So,
typeof Object
// evaluates:
typeof Object.__proto__ // "function"
typeof Object.__proto__.__proto__
// evaluates:
typeof Object.__proto__.__proto__.__proto__ // "object"
So when you call its prototype:
typeof Object.prototype
// evaluates:
typeof Object.prototype.__proto__ // "object"
This should satisfy your question 2 as well. Hope, you understand now.
And the following should throw you an error:
typeof Object.prototype.__proto__.__proto__ // TypeError
Because, at the end of its prototype chain, there's null
which is considered as object in JS but it cannot read the property __proto__
of null
.
but it is displaying a function as a "function" typeof(Foo) output: "function"
– vikrant
1 hour ago
Yes, function is as well considered object but is intrinsically a function.
– Bhojendra Rauniyar
1 hour ago
1
so everything is a function behind the scenes, because everywhere it is mentioned that in JS everything is an Object. "function is as well considered object but is intrinsically a function", i am not able to understand that part sorry
– vikrant
1 hour ago
Just remember function is nothing but an object.
– Bhojendra Rauniyar
47 mins ago
but how exactly? :) i don't understand what do you mean by "considers"? if it is a function, then it is a function, why is it considered as an object, is it converted to an object by js engine?
– vikrant
45 mins ago
|
show 3 more comments
Unlike other languages, JavaScript considers "function" as an object:
typeof Object // "function"
class Test {}
typeof Test // "function"
But null returns a "object":
typeof null // "object" => bad
If you need to consider intrinsic "object", you'll need to get it from prototype
:
typeof Object.prototype // "object"
typof Object.__proto__ // "function"
__proto__
is an internal function. But prototype
is a mechanism to get its parent definitions. So, using __proto__
will return the type as a function
while prototype
will return object
checking its definition.
So, calling Object.prototype
is nothing but it's just getting the name from it's constructor Object.prototype.constructor.name
.
So, if you do deeper drive, then you'll get "object" after following __proto__
:
typeof Object.__proto__.__proto__.__proto__ // "object"
At this point, you're at getting an object by checking it's definition. That's it. - Though, I am not able to clear up things to you.
Let me re-visit your question:
When you call typeof Object
, the JS will run its __proto__
.
So,
typeof Object
// evaluates:
typeof Object.__proto__ // "function"
typeof Object.__proto__.__proto__
// evaluates:
typeof Object.__proto__.__proto__.__proto__ // "object"
So when you call its prototype:
typeof Object.prototype
// evaluates:
typeof Object.prototype.__proto__ // "object"
This should satisfy your question 2 as well. Hope, you understand now.
And the following should throw you an error:
typeof Object.prototype.__proto__.__proto__ // TypeError
Because, at the end of its prototype chain, there's null
which is considered as object in JS but it cannot read the property __proto__
of null
.
Unlike other languages, JavaScript considers "function" as an object:
typeof Object // "function"
class Test {}
typeof Test // "function"
But null returns a "object":
typeof null // "object" => bad
If you need to consider intrinsic "object", you'll need to get it from prototype
:
typeof Object.prototype // "object"
typof Object.__proto__ // "function"
__proto__
is an internal function. But prototype
is a mechanism to get its parent definitions. So, using __proto__
will return the type as a function
while prototype
will return object
checking its definition.
So, calling Object.prototype
is nothing but it's just getting the name from it's constructor Object.prototype.constructor.name
.
So, if you do deeper drive, then you'll get "object" after following __proto__
:
typeof Object.__proto__.__proto__.__proto__ // "object"
At this point, you're at getting an object by checking it's definition. That's it. - Though, I am not able to clear up things to you.
Let me re-visit your question:
When you call typeof Object
, the JS will run its __proto__
.
So,
typeof Object
// evaluates:
typeof Object.__proto__ // "function"
typeof Object.__proto__.__proto__
// evaluates:
typeof Object.__proto__.__proto__.__proto__ // "object"
So when you call its prototype:
typeof Object.prototype
// evaluates:
typeof Object.prototype.__proto__ // "object"
This should satisfy your question 2 as well. Hope, you understand now.
And the following should throw you an error:
typeof Object.prototype.__proto__.__proto__ // TypeError
Because, at the end of its prototype chain, there's null
which is considered as object in JS but it cannot read the property __proto__
of null
.
edited 9 mins ago
answered 1 hour ago
Bhojendra RauniyarBhojendra Rauniyar
51.6k2079127
51.6k2079127
but it is displaying a function as a "function" typeof(Foo) output: "function"
– vikrant
1 hour ago
Yes, function is as well considered object but is intrinsically a function.
– Bhojendra Rauniyar
1 hour ago
1
so everything is a function behind the scenes, because everywhere it is mentioned that in JS everything is an Object. "function is as well considered object but is intrinsically a function", i am not able to understand that part sorry
– vikrant
1 hour ago
Just remember function is nothing but an object.
– Bhojendra Rauniyar
47 mins ago
but how exactly? :) i don't understand what do you mean by "considers"? if it is a function, then it is a function, why is it considered as an object, is it converted to an object by js engine?
– vikrant
45 mins ago
|
show 3 more comments
but it is displaying a function as a "function" typeof(Foo) output: "function"
– vikrant
1 hour ago
Yes, function is as well considered object but is intrinsically a function.
– Bhojendra Rauniyar
1 hour ago
1
so everything is a function behind the scenes, because everywhere it is mentioned that in JS everything is an Object. "function is as well considered object but is intrinsically a function", i am not able to understand that part sorry
– vikrant
1 hour ago
Just remember function is nothing but an object.
– Bhojendra Rauniyar
47 mins ago
but how exactly? :) i don't understand what do you mean by "considers"? if it is a function, then it is a function, why is it considered as an object, is it converted to an object by js engine?
– vikrant
45 mins ago
but it is displaying a function as a "function" typeof(Foo) output: "function"
– vikrant
1 hour ago
but it is displaying a function as a "function" typeof(Foo) output: "function"
– vikrant
1 hour ago
Yes, function is as well considered object but is intrinsically a function.
– Bhojendra Rauniyar
1 hour ago
Yes, function is as well considered object but is intrinsically a function.
– Bhojendra Rauniyar
1 hour ago
1
1
so everything is a function behind the scenes, because everywhere it is mentioned that in JS everything is an Object. "function is as well considered object but is intrinsically a function", i am not able to understand that part sorry
– vikrant
1 hour ago
so everything is a function behind the scenes, because everywhere it is mentioned that in JS everything is an Object. "function is as well considered object but is intrinsically a function", i am not able to understand that part sorry
– vikrant
1 hour ago
Just remember function is nothing but an object.
– Bhojendra Rauniyar
47 mins ago
Just remember function is nothing but an object.
– Bhojendra Rauniyar
47 mins ago
but how exactly? :) i don't understand what do you mean by "considers"? if it is a function, then it is a function, why is it considered as an object, is it converted to an object by js engine?
– vikrant
45 mins ago
but how exactly? :) i don't understand what do you mean by "considers"? if it is a function, then it is a function, why is it considered as an object, is it converted to an object by js engine?
– vikrant
45 mins ago
|
show 3 more comments
Q1: why are they pointing to Function?
A1: Because they are functions. Function
and Object
are just constructor functions.
Q2: If everything is an object in JS, then why is Object a function?
A2: Because Object
is just a constructor function.
typeof Object
// 'function'
typeof new Object()
// 'object'
And a function is a instance of Function
, so that makes a function object.
(function(){}).constructor === Function
// true
"And a function is a instance of Function, so that makes a function object." why does that make a function object?
– vikrant
40 mins ago
What I was trying to say is just,Function
constructs an object which is function.
– zmag
32 mins ago
if it is a function, why is it called object?
– vikrant
28 mins ago
because it's an object ofFunction
. difference betweenFunction
andfunction
is a point.
– zmag
26 mins ago
add a comment |
Q1: why are they pointing to Function?
A1: Because they are functions. Function
and Object
are just constructor functions.
Q2: If everything is an object in JS, then why is Object a function?
A2: Because Object
is just a constructor function.
typeof Object
// 'function'
typeof new Object()
// 'object'
And a function is a instance of Function
, so that makes a function object.
(function(){}).constructor === Function
// true
"And a function is a instance of Function, so that makes a function object." why does that make a function object?
– vikrant
40 mins ago
What I was trying to say is just,Function
constructs an object which is function.
– zmag
32 mins ago
if it is a function, why is it called object?
– vikrant
28 mins ago
because it's an object ofFunction
. difference betweenFunction
andfunction
is a point.
– zmag
26 mins ago
add a comment |
Q1: why are they pointing to Function?
A1: Because they are functions. Function
and Object
are just constructor functions.
Q2: If everything is an object in JS, then why is Object a function?
A2: Because Object
is just a constructor function.
typeof Object
// 'function'
typeof new Object()
// 'object'
And a function is a instance of Function
, so that makes a function object.
(function(){}).constructor === Function
// true
Q1: why are they pointing to Function?
A1: Because they are functions. Function
and Object
are just constructor functions.
Q2: If everything is an object in JS, then why is Object a function?
A2: Because Object
is just a constructor function.
typeof Object
// 'function'
typeof new Object()
// 'object'
And a function is a instance of Function
, so that makes a function object.
(function(){}).constructor === Function
// true
edited 45 mins ago
answered 51 mins ago
zmagzmag
1,119618
1,119618
"And a function is a instance of Function, so that makes a function object." why does that make a function object?
– vikrant
40 mins ago
What I was trying to say is just,Function
constructs an object which is function.
– zmag
32 mins ago
if it is a function, why is it called object?
– vikrant
28 mins ago
because it's an object ofFunction
. difference betweenFunction
andfunction
is a point.
– zmag
26 mins ago
add a comment |
"And a function is a instance of Function, so that makes a function object." why does that make a function object?
– vikrant
40 mins ago
What I was trying to say is just,Function
constructs an object which is function.
– zmag
32 mins ago
if it is a function, why is it called object?
– vikrant
28 mins ago
because it's an object ofFunction
. difference betweenFunction
andfunction
is a point.
– zmag
26 mins ago
"And a function is a instance of Function, so that makes a function object." why does that make a function object?
– vikrant
40 mins ago
"And a function is a instance of Function, so that makes a function object." why does that make a function object?
– vikrant
40 mins ago
What I was trying to say is just,
Function
constructs an object which is function.– zmag
32 mins ago
What I was trying to say is just,
Function
constructs an object which is function.– zmag
32 mins ago
if it is a function, why is it called object?
– vikrant
28 mins ago
if it is a function, why is it called object?
– vikrant
28 mins ago
because it's an object of
Function
. difference between Function
and function
is a point.– zmag
26 mins ago
because it's an object of
Function
. difference between Function
and function
is a point.– zmag
26 mins ago
add a comment |
Object
is the constructor function of all objects. So, typeof Object==="function"
Here is a snippet for visualisation:
console.log(typeof Object) //function (which is an object)
var object=new Object() //An instance of Object 'class'
console.log(typeof object) //object
console.log(object instanceof Object) //true, because object is created by Object()
Function
is the constructor function of all functions (including itself...)
So, although not everything is a function, but most of Uppercase native identifiers refers to a constructor function.
add a comment |
Object
is the constructor function of all objects. So, typeof Object==="function"
Here is a snippet for visualisation:
console.log(typeof Object) //function (which is an object)
var object=new Object() //An instance of Object 'class'
console.log(typeof object) //object
console.log(object instanceof Object) //true, because object is created by Object()
Function
is the constructor function of all functions (including itself...)
So, although not everything is a function, but most of Uppercase native identifiers refers to a constructor function.
add a comment |
Object
is the constructor function of all objects. So, typeof Object==="function"
Here is a snippet for visualisation:
console.log(typeof Object) //function (which is an object)
var object=new Object() //An instance of Object 'class'
console.log(typeof object) //object
console.log(object instanceof Object) //true, because object is created by Object()
Function
is the constructor function of all functions (including itself...)
So, although not everything is a function, but most of Uppercase native identifiers refers to a constructor function.
Object
is the constructor function of all objects. So, typeof Object==="function"
Here is a snippet for visualisation:
console.log(typeof Object) //function (which is an object)
var object=new Object() //An instance of Object 'class'
console.log(typeof object) //object
console.log(object instanceof Object) //true, because object is created by Object()
Function
is the constructor function of all functions (including itself...)
So, although not everything is a function, but most of Uppercase native identifiers refers to a constructor function.
console.log(typeof Object) //function (which is an object)
var object=new Object() //An instance of Object 'class'
console.log(typeof object) //object
console.log(object instanceof Object) //true, because object is created by Object()
console.log(typeof Object) //function (which is an object)
var object=new Object() //An instance of Object 'class'
console.log(typeof object) //object
console.log(object instanceof Object) //true, because object is created by Object()
answered 40 mins ago
FZsFZs
1,8112824
1,8112824
add a comment |
add a comment |
Fundamentally
Functions
has some code that can be executed.
Object
are those which contains data.
For class Point
having x
and y
.
class Point {
constructor(x, y) {
this.x = x;
this.y = y;
}
isOrigin() { return x == 0 && y == 0; }
}
let p = new Point();
Answer 1
in this p
is object
which contains data or other functions.
p.isOrigin
is function.
Similarly class Point
is itself a function
which when runs produces p
. Thats why all classes like Object
and Functions
are functions
more precisely constructor functions
.
Answer2
If everything is an object in JS, then why is Object a function?
Same as Answer1.
Also how is a function actually implemented inside JS?
Different js engine will have different implementations. They have specifications which they need to follow.
What happens to the variables declared inside a function?
Off topic. Every function
runs with a scope which holds all data for that functions.
Is a function converted into an object by JS compiler?
Not sure what are you asking.
add a comment |
Fundamentally
Functions
has some code that can be executed.
Object
are those which contains data.
For class Point
having x
and y
.
class Point {
constructor(x, y) {
this.x = x;
this.y = y;
}
isOrigin() { return x == 0 && y == 0; }
}
let p = new Point();
Answer 1
in this p
is object
which contains data or other functions.
p.isOrigin
is function.
Similarly class Point
is itself a function
which when runs produces p
. Thats why all classes like Object
and Functions
are functions
more precisely constructor functions
.
Answer2
If everything is an object in JS, then why is Object a function?
Same as Answer1.
Also how is a function actually implemented inside JS?
Different js engine will have different implementations. They have specifications which they need to follow.
What happens to the variables declared inside a function?
Off topic. Every function
runs with a scope which holds all data for that functions.
Is a function converted into an object by JS compiler?
Not sure what are you asking.
add a comment |
Fundamentally
Functions
has some code that can be executed.
Object
are those which contains data.
For class Point
having x
and y
.
class Point {
constructor(x, y) {
this.x = x;
this.y = y;
}
isOrigin() { return x == 0 && y == 0; }
}
let p = new Point();
Answer 1
in this p
is object
which contains data or other functions.
p.isOrigin
is function.
Similarly class Point
is itself a function
which when runs produces p
. Thats why all classes like Object
and Functions
are functions
more precisely constructor functions
.
Answer2
If everything is an object in JS, then why is Object a function?
Same as Answer1.
Also how is a function actually implemented inside JS?
Different js engine will have different implementations. They have specifications which they need to follow.
What happens to the variables declared inside a function?
Off topic. Every function
runs with a scope which holds all data for that functions.
Is a function converted into an object by JS compiler?
Not sure what are you asking.
Fundamentally
Functions
has some code that can be executed.
Object
are those which contains data.
For class Point
having x
and y
.
class Point {
constructor(x, y) {
this.x = x;
this.y = y;
}
isOrigin() { return x == 0 && y == 0; }
}
let p = new Point();
Answer 1
in this p
is object
which contains data or other functions.
p.isOrigin
is function.
Similarly class Point
is itself a function
which when runs produces p
. Thats why all classes like Object
and Functions
are functions
more precisely constructor functions
.
Answer2
If everything is an object in JS, then why is Object a function?
Same as Answer1.
Also how is a function actually implemented inside JS?
Different js engine will have different implementations. They have specifications which they need to follow.
What happens to the variables declared inside a function?
Off topic. Every function
runs with a scope which holds all data for that functions.
Is a function converted into an object by JS compiler?
Not sure what are you asking.
answered 32 mins ago
amit77309amit77309
3,73931118
3,73931118
add a comment |
add a comment |
Function and Object are both constructor function which can be used to create a function and an object respectively which is the reason typeof Function
returns function
About the similarities and differences between and object and a function, consider the following points:
- All non primitive types are objects in javascript.
- All objects indirectly inherit from Object.prototype which inherits from null.
- All native functions inherit from Function.prototype which inherits from Object.prototype so it means function indirectly inherits from Object.prototype.
- A function can be called using
()
operator because javascript engine knows it is declared using a function keyword and it inherits from Function.prototype and have executable code, so whenever it is called, javascript engine creates a new execution context and set thethis
binding and then executes the function. None of that happens when you try to call an object instead an error is thrown i.e. "is not a function". So we can say that not every object is a function because not all objects inherit from Function.prototype but all objects do inherit from Object.prototype. - As function indirectly inherits from Object.prototype, you can treat it as an object e.g. add properties to it, create new objects from it.
- Because a function indirectly inherits from Object.prototype, we call it an object but it is different from other objects because it inherits directly from Function.prototype and has code that can be executed.
add a comment |
Function and Object are both constructor function which can be used to create a function and an object respectively which is the reason typeof Function
returns function
About the similarities and differences between and object and a function, consider the following points:
- All non primitive types are objects in javascript.
- All objects indirectly inherit from Object.prototype which inherits from null.
- All native functions inherit from Function.prototype which inherits from Object.prototype so it means function indirectly inherits from Object.prototype.
- A function can be called using
()
operator because javascript engine knows it is declared using a function keyword and it inherits from Function.prototype and have executable code, so whenever it is called, javascript engine creates a new execution context and set thethis
binding and then executes the function. None of that happens when you try to call an object instead an error is thrown i.e. "is not a function". So we can say that not every object is a function because not all objects inherit from Function.prototype but all objects do inherit from Object.prototype. - As function indirectly inherits from Object.prototype, you can treat it as an object e.g. add properties to it, create new objects from it.
- Because a function indirectly inherits from Object.prototype, we call it an object but it is different from other objects because it inherits directly from Function.prototype and has code that can be executed.
add a comment |
Function and Object are both constructor function which can be used to create a function and an object respectively which is the reason typeof Function
returns function
About the similarities and differences between and object and a function, consider the following points:
- All non primitive types are objects in javascript.
- All objects indirectly inherit from Object.prototype which inherits from null.
- All native functions inherit from Function.prototype which inherits from Object.prototype so it means function indirectly inherits from Object.prototype.
- A function can be called using
()
operator because javascript engine knows it is declared using a function keyword and it inherits from Function.prototype and have executable code, so whenever it is called, javascript engine creates a new execution context and set thethis
binding and then executes the function. None of that happens when you try to call an object instead an error is thrown i.e. "is not a function". So we can say that not every object is a function because not all objects inherit from Function.prototype but all objects do inherit from Object.prototype. - As function indirectly inherits from Object.prototype, you can treat it as an object e.g. add properties to it, create new objects from it.
- Because a function indirectly inherits from Object.prototype, we call it an object but it is different from other objects because it inherits directly from Function.prototype and has code that can be executed.
Function and Object are both constructor function which can be used to create a function and an object respectively which is the reason typeof Function
returns function
About the similarities and differences between and object and a function, consider the following points:
- All non primitive types are objects in javascript.
- All objects indirectly inherit from Object.prototype which inherits from null.
- All native functions inherit from Function.prototype which inherits from Object.prototype so it means function indirectly inherits from Object.prototype.
- A function can be called using
()
operator because javascript engine knows it is declared using a function keyword and it inherits from Function.prototype and have executable code, so whenever it is called, javascript engine creates a new execution context and set thethis
binding and then executes the function. None of that happens when you try to call an object instead an error is thrown i.e. "is not a function". So we can say that not every object is a function because not all objects inherit from Function.prototype but all objects do inherit from Object.prototype. - As function indirectly inherits from Object.prototype, you can treat it as an object e.g. add properties to it, create new objects from it.
- Because a function indirectly inherits from Object.prototype, we call it an object but it is different from other objects because it inherits directly from Function.prototype and has code that can be executed.
answered 3 mins ago
Nabil ShahidNabil Shahid
67826
67826
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54861385%2fis-object-a-function-in-javascript%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
"typeof Object" Of course, it is a function because this is constructor function. Even
Number
, orBoolean
are functions.– dfsq
1 hour ago
Do note that there is a difference between "object" (the concept) and the function "Object". In JavaScript, all functions are objects, including "Object". On the other hand not all objects are functions (eg.
var a = {}
)– TiiJ7
54 mins ago
@TiiJ7 "In JavaScript, all functions are objects, including "Object" , how exactly? we can use dot operator with an object but not with a function to access it's local variables
– vikrant
41 mins ago
You are doing exactly that, though:
Object.__proto__
=> You get the property__proto__
fromObject
, which is afunction
and thus anobject
.– TiiJ7
39 mins ago
1
Just to make it sure, see another fiddle = ).
– Teemu
4 mins ago