Is `Object` a function in javascript?__proto__ VS. prototype in JavaScriptHow do JavaScript closures...
What does からか mean?
Is there any danger of my neighbor having my wife's signature?
How to fly a direct entry holding pattern when approaching from an awkward angle?
Prevent Nautilus / Nemo from creating .Trash-1000 folder in mounted devices
Eww, those bytes are gross
How to not let the Identify spell spoil everything?
Word for something that's always reliable, but never the best?
Case protection with emphasis in biblatex
What's the oldest plausible frozen specimen for a Jurassic Park style story-line?
What are some ways of extending a description of a scenery?
Count repetitions of an array
Why is Shelob considered evil?
How can I give a Ranger advantage on a check due to Favored Enemy without spoiling the story for the player?
Why is 'diphthong' not pronounced otherwise?
Why did Luke use his left hand to shoot?
Caron Accent v{a} doesn't render without usepackage{xeCJK}
Coworker asking me to not bring cakes due to self control issue. What should I do?
How to create a label containing values from different layers in QGIS
Is Screenshot Time-tracking Common?
How vim overwrites readonly mode?
How to completely remove a package in Ubuntu (like it never existed)
How do you get out of your own psychology to write characters?
Why do neural networks need so many examples to perform?
What species should be used for storage of human minds?
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 12 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
7 hours 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
7 hours 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
6 hours ago
You are doing exactly that, though:Object.__proto__
=> You get the property__proto__
fromObject
, which is afunction
and thus anobject
.
– TiiJ7
6 hours ago
1
Just to make it sure, see another fiddle = ).
– Teemu
6 hours ago
|
show 12 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 4 hours ago
vikrant
asked 7 hours ago
vikrantvikrant
548616
548616
"typeof Object" Of course, it is a function because this is constructor function. EvenNumber
, orBoolean
are functions.
– dfsq
7 hours 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
7 hours 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
6 hours ago
You are doing exactly that, though:Object.__proto__
=> You get the property__proto__
fromObject
, which is afunction
and thus anobject
.
– TiiJ7
6 hours ago
1
Just to make it sure, see another fiddle = ).
– Teemu
6 hours ago
|
show 12 more comments
"typeof Object" Of course, it is a function because this is constructor function. EvenNumber
, orBoolean
are functions.
– dfsq
7 hours 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
7 hours 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
6 hours ago
You are doing exactly that, though:Object.__proto__
=> You get the property__proto__
fromObject
, which is afunction
and thus anobject
.
– TiiJ7
6 hours ago
1
Just to make it sure, see another fiddle = ).
– Teemu
6 hours ago
"typeof Object" Of course, it is a function because this is constructor function. Even
Number
, or Boolean
are functions.– dfsq
7 hours ago
"typeof Object" Of course, it is a function because this is constructor function. Even
Number
, or Boolean
are functions.– dfsq
7 hours 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
7 hours 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
7 hours 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
6 hours 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
6 hours 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
6 hours 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
6 hours ago
1
1
Just to make it sure, see another fiddle = ).
– Teemu
6 hours ago
Just to make it sure, see another fiddle = ).
– Teemu
6 hours ago
|
show 12 more comments
6 Answers
6
active
oldest
votes
You seem to be confused between the two concepts object
and Object
.
An object
is a concept in JavaScript that is a generic container for some data. An object
contains properties with various data in them.
In JavaScript, everything (other than primitives) is an object
. This includes function
s, which are basically a special type of object
that can be "called" with the ()
syntax.
JavaScript provides a number of built-in function
s that have various purposes. Two such functions happen to be called Object
and Function
. So in other words Object
is a function
and thus also an "object" (the concept).
Let's take your function Foo
as an example:
function Foo() {
var a = "3";
}
Foo
is a function
. This means that Foo
can be called, eg. var f = Foo()
. In this case f
will be undefined
since Foo
doesn't return anything.
Because Foo
is a function
, it is also an object
. This means we can also add and read properties from it:
Foo.bar = 5;
Foo.bar++;
console.log(Foo.bar); // prints 6
Please note that this "object" part of Foo
is not related to the contents of the function. That means that the code you declared (var a = "3"
) is irrelevant. You cannot access var a
in any way here because it does not exist until you call the function. If you were to do Foo.a
, you are not manipulating var a
inside the function; you are working with the property a
on the object Foo
.
You can however do it the other way around and access properties on Foo
inside of the function:
function Foo() {
var a = "3"; // a is local to this scope, you cannot get to it from outside
console.log(a); // prints 3 - local variable a is accessible inside the scope of this function
console.log(Foo.a); // prints 5 - a is a property on object Foo, and is accessible here
}
// var a inside Foo cannot be accessed here
Foo.a = 5;
Foo();
Edit: Re. your question regarding "this" in the comments. this
is a special keyword in JavaScript that refers to an object. However, this object is not the function itself, it is a new object that is created when you call a function
using the new
keyword:
function Bar() {
this.a = 10;
console.log(this == Bar); // prints false
}
var bar = new Bar();
console.log(bar.a); // prints 10
This is referred to as a "constructor function". In that case, the prototype of the created object (bar.__proto__
) is also set to the property "prototype
" of this function:
console.log(bar.constructor == Bar) // prints true
console.log(bar.__proto__ == Bar.prototype) // prints true
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.
consider this fiddle, jsfiddle.net/rs2y1qg9/3 why doesn't the this keyword work inside when i call the function?i understand that this must point to an object, but if the function is an object, then the context should be set for this
– vikrant
6 hours ago
Close this question if answered. Ask another question.this
keyword itself is a huge topic. medium.com/quick-code/…
– amit77309
5 hours ago
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.
I hope it clears both questions.
add a comment |
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"
// which is equivalents to:
typeof Object.__proto__.__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
7 hours ago
Yes, function is as well considered object but is intrinsically a function.
– Bhojendra Rauniyar
7 hours 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
7 hours ago
Just remember function is nothing but an object.
– Bhojendra Rauniyar
7 hours 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
7 hours 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
6 hours ago
What I was trying to say is just,Function
constructs an object which is function.
– zmag
6 hours ago
if it is a function, why is it called object?
– vikrant
6 hours ago
because it's an object ofFunction
. difference betweenFunction
andfunction
is a point.
– zmag
6 hours 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 |
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
6 Answers
6
active
oldest
votes
6 Answers
6
active
oldest
votes
active
oldest
votes
active
oldest
votes
You seem to be confused between the two concepts object
and Object
.
An object
is a concept in JavaScript that is a generic container for some data. An object
contains properties with various data in them.
In JavaScript, everything (other than primitives) is an object
. This includes function
s, which are basically a special type of object
that can be "called" with the ()
syntax.
JavaScript provides a number of built-in function
s that have various purposes. Two such functions happen to be called Object
and Function
. So in other words Object
is a function
and thus also an "object" (the concept).
Let's take your function Foo
as an example:
function Foo() {
var a = "3";
}
Foo
is a function
. This means that Foo
can be called, eg. var f = Foo()
. In this case f
will be undefined
since Foo
doesn't return anything.
Because Foo
is a function
, it is also an object
. This means we can also add and read properties from it:
Foo.bar = 5;
Foo.bar++;
console.log(Foo.bar); // prints 6
Please note that this "object" part of Foo
is not related to the contents of the function. That means that the code you declared (var a = "3"
) is irrelevant. You cannot access var a
in any way here because it does not exist until you call the function. If you were to do Foo.a
, you are not manipulating var a
inside the function; you are working with the property a
on the object Foo
.
You can however do it the other way around and access properties on Foo
inside of the function:
function Foo() {
var a = "3"; // a is local to this scope, you cannot get to it from outside
console.log(a); // prints 3 - local variable a is accessible inside the scope of this function
console.log(Foo.a); // prints 5 - a is a property on object Foo, and is accessible here
}
// var a inside Foo cannot be accessed here
Foo.a = 5;
Foo();
Edit: Re. your question regarding "this" in the comments. this
is a special keyword in JavaScript that refers to an object. However, this object is not the function itself, it is a new object that is created when you call a function
using the new
keyword:
function Bar() {
this.a = 10;
console.log(this == Bar); // prints false
}
var bar = new Bar();
console.log(bar.a); // prints 10
This is referred to as a "constructor function". In that case, the prototype of the created object (bar.__proto__
) is also set to the property "prototype
" of this function:
console.log(bar.constructor == Bar) // prints true
console.log(bar.__proto__ == Bar.prototype) // prints true
add a comment |
You seem to be confused between the two concepts object
and Object
.
An object
is a concept in JavaScript that is a generic container for some data. An object
contains properties with various data in them.
In JavaScript, everything (other than primitives) is an object
. This includes function
s, which are basically a special type of object
that can be "called" with the ()
syntax.
JavaScript provides a number of built-in function
s that have various purposes. Two such functions happen to be called Object
and Function
. So in other words Object
is a function
and thus also an "object" (the concept).
Let's take your function Foo
as an example:
function Foo() {
var a = "3";
}
Foo
is a function
. This means that Foo
can be called, eg. var f = Foo()
. In this case f
will be undefined
since Foo
doesn't return anything.
Because Foo
is a function
, it is also an object
. This means we can also add and read properties from it:
Foo.bar = 5;
Foo.bar++;
console.log(Foo.bar); // prints 6
Please note that this "object" part of Foo
is not related to the contents of the function. That means that the code you declared (var a = "3"
) is irrelevant. You cannot access var a
in any way here because it does not exist until you call the function. If you were to do Foo.a
, you are not manipulating var a
inside the function; you are working with the property a
on the object Foo
.
You can however do it the other way around and access properties on Foo
inside of the function:
function Foo() {
var a = "3"; // a is local to this scope, you cannot get to it from outside
console.log(a); // prints 3 - local variable a is accessible inside the scope of this function
console.log(Foo.a); // prints 5 - a is a property on object Foo, and is accessible here
}
// var a inside Foo cannot be accessed here
Foo.a = 5;
Foo();
Edit: Re. your question regarding "this" in the comments. this
is a special keyword in JavaScript that refers to an object. However, this object is not the function itself, it is a new object that is created when you call a function
using the new
keyword:
function Bar() {
this.a = 10;
console.log(this == Bar); // prints false
}
var bar = new Bar();
console.log(bar.a); // prints 10
This is referred to as a "constructor function". In that case, the prototype of the created object (bar.__proto__
) is also set to the property "prototype
" of this function:
console.log(bar.constructor == Bar) // prints true
console.log(bar.__proto__ == Bar.prototype) // prints true
add a comment |
You seem to be confused between the two concepts object
and Object
.
An object
is a concept in JavaScript that is a generic container for some data. An object
contains properties with various data in them.
In JavaScript, everything (other than primitives) is an object
. This includes function
s, which are basically a special type of object
that can be "called" with the ()
syntax.
JavaScript provides a number of built-in function
s that have various purposes. Two such functions happen to be called Object
and Function
. So in other words Object
is a function
and thus also an "object" (the concept).
Let's take your function Foo
as an example:
function Foo() {
var a = "3";
}
Foo
is a function
. This means that Foo
can be called, eg. var f = Foo()
. In this case f
will be undefined
since Foo
doesn't return anything.
Because Foo
is a function
, it is also an object
. This means we can also add and read properties from it:
Foo.bar = 5;
Foo.bar++;
console.log(Foo.bar); // prints 6
Please note that this "object" part of Foo
is not related to the contents of the function. That means that the code you declared (var a = "3"
) is irrelevant. You cannot access var a
in any way here because it does not exist until you call the function. If you were to do Foo.a
, you are not manipulating var a
inside the function; you are working with the property a
on the object Foo
.
You can however do it the other way around and access properties on Foo
inside of the function:
function Foo() {
var a = "3"; // a is local to this scope, you cannot get to it from outside
console.log(a); // prints 3 - local variable a is accessible inside the scope of this function
console.log(Foo.a); // prints 5 - a is a property on object Foo, and is accessible here
}
// var a inside Foo cannot be accessed here
Foo.a = 5;
Foo();
Edit: Re. your question regarding "this" in the comments. this
is a special keyword in JavaScript that refers to an object. However, this object is not the function itself, it is a new object that is created when you call a function
using the new
keyword:
function Bar() {
this.a = 10;
console.log(this == Bar); // prints false
}
var bar = new Bar();
console.log(bar.a); // prints 10
This is referred to as a "constructor function". In that case, the prototype of the created object (bar.__proto__
) is also set to the property "prototype
" of this function:
console.log(bar.constructor == Bar) // prints true
console.log(bar.__proto__ == Bar.prototype) // prints true
You seem to be confused between the two concepts object
and Object
.
An object
is a concept in JavaScript that is a generic container for some data. An object
contains properties with various data in them.
In JavaScript, everything (other than primitives) is an object
. This includes function
s, which are basically a special type of object
that can be "called" with the ()
syntax.
JavaScript provides a number of built-in function
s that have various purposes. Two such functions happen to be called Object
and Function
. So in other words Object
is a function
and thus also an "object" (the concept).
Let's take your function Foo
as an example:
function Foo() {
var a = "3";
}
Foo
is a function
. This means that Foo
can be called, eg. var f = Foo()
. In this case f
will be undefined
since Foo
doesn't return anything.
Because Foo
is a function
, it is also an object
. This means we can also add and read properties from it:
Foo.bar = 5;
Foo.bar++;
console.log(Foo.bar); // prints 6
Please note that this "object" part of Foo
is not related to the contents of the function. That means that the code you declared (var a = "3"
) is irrelevant. You cannot access var a
in any way here because it does not exist until you call the function. If you were to do Foo.a
, you are not manipulating var a
inside the function; you are working with the property a
on the object Foo
.
You can however do it the other way around and access properties on Foo
inside of the function:
function Foo() {
var a = "3"; // a is local to this scope, you cannot get to it from outside
console.log(a); // prints 3 - local variable a is accessible inside the scope of this function
console.log(Foo.a); // prints 5 - a is a property on object Foo, and is accessible here
}
// var a inside Foo cannot be accessed here
Foo.a = 5;
Foo();
Edit: Re. your question regarding "this" in the comments. this
is a special keyword in JavaScript that refers to an object. However, this object is not the function itself, it is a new object that is created when you call a function
using the new
keyword:
function Bar() {
this.a = 10;
console.log(this == Bar); // prints false
}
var bar = new Bar();
console.log(bar.a); // prints 10
This is referred to as a "constructor function". In that case, the prototype of the created object (bar.__proto__
) is also set to the property "prototype
" of this function:
console.log(bar.constructor == Bar) // prints true
console.log(bar.__proto__ == Bar.prototype) // prints true
edited 5 hours ago
answered 6 hours ago
TiiJ7TiiJ7
2,07211224
2,07211224
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.
consider this fiddle, jsfiddle.net/rs2y1qg9/3 why doesn't the this keyword work inside when i call the function?i understand that this must point to an object, but if the function is an object, then the context should be set for this
– vikrant
6 hours ago
Close this question if answered. Ask another question.this
keyword itself is a huge topic. medium.com/quick-code/…
– amit77309
5 hours ago
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.
consider this fiddle, jsfiddle.net/rs2y1qg9/3 why doesn't the this keyword work inside when i call the function?i understand that this must point to an object, but if the function is an object, then the context should be set for this
– vikrant
6 hours ago
Close this question if answered. Ask another question.this
keyword itself is a huge topic. medium.com/quick-code/…
– amit77309
5 hours ago
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 6 hours ago
amit77309amit77309
3,76931118
3,76931118
consider this fiddle, jsfiddle.net/rs2y1qg9/3 why doesn't the this keyword work inside when i call the function?i understand that this must point to an object, but if the function is an object, then the context should be set for this
– vikrant
6 hours ago
Close this question if answered. Ask another question.this
keyword itself is a huge topic. medium.com/quick-code/…
– amit77309
5 hours ago
add a comment |
consider this fiddle, jsfiddle.net/rs2y1qg9/3 why doesn't the this keyword work inside when i call the function?i understand that this must point to an object, but if the function is an object, then the context should be set for this
– vikrant
6 hours ago
Close this question if answered. Ask another question.this
keyword itself is a huge topic. medium.com/quick-code/…
– amit77309
5 hours ago
consider this fiddle, jsfiddle.net/rs2y1qg9/3 why doesn't the this keyword work inside when i call the function?i understand that this must point to an object, but if the function is an object, then the context should be set for this
– vikrant
6 hours ago
consider this fiddle, jsfiddle.net/rs2y1qg9/3 why doesn't the this keyword work inside when i call the function?i understand that this must point to an object, but if the function is an object, then the context should be set for this
– vikrant
6 hours ago
Close this question if answered. Ask another question.
this
keyword itself is a huge topic. medium.com/quick-code/…– amit77309
5 hours ago
Close this question if answered. Ask another question.
this
keyword itself is a huge topic. medium.com/quick-code/…– amit77309
5 hours ago
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.
I hope it clears both questions.
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.
I hope it clears both questions.
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.
I hope it clears both questions.
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.
I hope it clears both questions.
edited 6 hours ago
answered 6 hours ago
Nabil ShahidNabil Shahid
70826
70826
add a comment |
add a comment |
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"
// which is equivalents to:
typeof Object.__proto__.__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
7 hours ago
Yes, function is as well considered object but is intrinsically a function.
– Bhojendra Rauniyar
7 hours 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
7 hours ago
Just remember function is nothing but an object.
– Bhojendra Rauniyar
7 hours 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
7 hours 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"
// which is equivalents to:
typeof Object.__proto__.__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
7 hours ago
Yes, function is as well considered object but is intrinsically a function.
– Bhojendra Rauniyar
7 hours 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
7 hours ago
Just remember function is nothing but an object.
– Bhojendra Rauniyar
7 hours 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
7 hours 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"
// which is equivalents to:
typeof Object.__proto__.__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"
// which is equivalents to:
typeof Object.__proto__.__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 6 hours ago
answered 7 hours ago
Bhojendra RauniyarBhojendra Rauniyar
51.6k2079127
51.6k2079127
but it is displaying a function as a "function" typeof(Foo) output: "function"
– vikrant
7 hours ago
Yes, function is as well considered object but is intrinsically a function.
– Bhojendra Rauniyar
7 hours 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
7 hours ago
Just remember function is nothing but an object.
– Bhojendra Rauniyar
7 hours 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
7 hours ago
|
show 3 more comments
but it is displaying a function as a "function" typeof(Foo) output: "function"
– vikrant
7 hours ago
Yes, function is as well considered object but is intrinsically a function.
– Bhojendra Rauniyar
7 hours 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
7 hours ago
Just remember function is nothing but an object.
– Bhojendra Rauniyar
7 hours 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
7 hours ago
but it is displaying a function as a "function" typeof(Foo) output: "function"
– vikrant
7 hours ago
but it is displaying a function as a "function" typeof(Foo) output: "function"
– vikrant
7 hours ago
Yes, function is as well considered object but is intrinsically a function.
– Bhojendra Rauniyar
7 hours ago
Yes, function is as well considered object but is intrinsically a function.
– Bhojendra Rauniyar
7 hours 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
7 hours 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
7 hours ago
Just remember function is nothing but an object.
– Bhojendra Rauniyar
7 hours ago
Just remember function is nothing but an object.
– Bhojendra Rauniyar
7 hours 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
7 hours 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
7 hours 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
6 hours ago
What I was trying to say is just,Function
constructs an object which is function.
– zmag
6 hours ago
if it is a function, why is it called object?
– vikrant
6 hours ago
because it's an object ofFunction
. difference betweenFunction
andfunction
is a point.
– zmag
6 hours 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
6 hours ago
What I was trying to say is just,Function
constructs an object which is function.
– zmag
6 hours ago
if it is a function, why is it called object?
– vikrant
6 hours ago
because it's an object ofFunction
. difference betweenFunction
andfunction
is a point.
– zmag
6 hours 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 7 hours ago
answered 7 hours ago
zmagzmag
1,174618
1,174618
"And a function is a instance of Function, so that makes a function object." why does that make a function object?
– vikrant
6 hours ago
What I was trying to say is just,Function
constructs an object which is function.
– zmag
6 hours ago
if it is a function, why is it called object?
– vikrant
6 hours ago
because it's an object ofFunction
. difference betweenFunction
andfunction
is a point.
– zmag
6 hours 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
6 hours ago
What I was trying to say is just,Function
constructs an object which is function.
– zmag
6 hours ago
if it is a function, why is it called object?
– vikrant
6 hours ago
because it's an object ofFunction
. difference betweenFunction
andfunction
is a point.
– zmag
6 hours ago
"And a function is a instance of Function, so that makes a function object." why does that make a function object?
– vikrant
6 hours ago
"And a function is a instance of Function, so that makes a function object." why does that make a function object?
– vikrant
6 hours ago
What I was trying to say is just,
Function
constructs an object which is function.– zmag
6 hours ago
What I was trying to say is just,
Function
constructs an object which is function.– zmag
6 hours ago
if it is a function, why is it called object?
– vikrant
6 hours ago
if it is a function, why is it called object?
– vikrant
6 hours ago
because it's an object of
Function
. difference between Function
and function
is a point.– zmag
6 hours ago
because it's an object of
Function
. difference between Function
and function
is a point.– zmag
6 hours 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 6 hours ago
FZsFZs
1,8112824
1,8112824
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
7 hours 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
7 hours 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
6 hours ago
You are doing exactly that, though:
Object.__proto__
=> You get the property__proto__
fromObject
, which is afunction
and thus anobject
.– TiiJ7
6 hours ago
1
Just to make it sure, see another fiddle = ).
– Teemu
6 hours ago