Is `Object` a function in JavaScript?
Clash Royale CLAN TAG#URR8PPP
Consider this function:
function Foo()
var a = "3";
;
According to __proto__ VS. prototype in JavaScript,
Foo.__proto__ = Function.prototype
Function.prototype.__proto__ = Object.prototype
I understood that part, but if I do this in the Google 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 JavaScript, then why is Object
a function? Also, how is a function actually implemented inside JavaScript? What happens to the variables declared inside a function? Is a function converted into an object by the JavaScript compiler?
Sorry if I am missing something obvious. I am really confused by the way function and object are implemented in JavaScript.
javascript prototype
|
show 17 more comments
Consider this function:
function Foo()
var a = "3";
;
According to __proto__ VS. prototype in JavaScript,
Foo.__proto__ = Function.prototype
Function.prototype.__proto__ = Object.prototype
I understood that part, but if I do this in the Google 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 JavaScript, then why is Object
a function? Also, how is a function actually implemented inside JavaScript? What happens to the variables declared inside a function? Is a function converted into an object by the JavaScript compiler?
Sorry if I am missing something obvious. I am really confused by the way function and object are implemented in JavaScript.
javascript prototype
"typeof Object" Of course, it is a function because this is constructor function. EvenNumber
, orBoolean
are functions.
– dfsq
Feb 25 at 7:33
2
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
Feb 25 at 7:46
2
You perfectly well can do eg.Foo.a = 5
. But be aware that this is independent of the variables you declare inside the function. The contents of the function are just a block of code that you associate with the function, its scope is not the "object" part that you are referring to.
– TiiJ7
Feb 25 at 8:15
2
You've messed up something in your tests. It's perfectly legit and working to add a property to a function. For example, jQuery's$
is a function, but you can call some methods of it, like$.each()
.
– Teemu
Feb 25 at 8:21
2
Just to make it sure, see another fiddle = ).
– Teemu
Feb 25 at 8:36
|
show 17 more comments
Consider this function:
function Foo()
var a = "3";
;
According to __proto__ VS. prototype in JavaScript,
Foo.__proto__ = Function.prototype
Function.prototype.__proto__ = Object.prototype
I understood that part, but if I do this in the Google 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 JavaScript, then why is Object
a function? Also, how is a function actually implemented inside JavaScript? What happens to the variables declared inside a function? Is a function converted into an object by the JavaScript compiler?
Sorry if I am missing something obvious. I am really confused by the way function and object are implemented in JavaScript.
javascript prototype
Consider this function:
function Foo()
var a = "3";
;
According to __proto__ VS. prototype in JavaScript,
Foo.__proto__ = Function.prototype
Function.prototype.__proto__ = Object.prototype
I understood that part, but if I do this in the Google 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 JavaScript, then why is Object
a function? Also, how is a function actually implemented inside JavaScript? What happens to the variables declared inside a function? Is a function converted into an object by the JavaScript compiler?
Sorry if I am missing something obvious. I am really confused by the way function and object are implemented in JavaScript.
javascript prototype
javascript prototype
edited Mar 17 at 14:23
flppv
1,617827
1,617827
asked Feb 25 at 7:30
vikrantvikrant
8521121
8521121
"typeof Object" Of course, it is a function because this is constructor function. EvenNumber
, orBoolean
are functions.
– dfsq
Feb 25 at 7:33
2
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
Feb 25 at 7:46
2
You perfectly well can do eg.Foo.a = 5
. But be aware that this is independent of the variables you declare inside the function. The contents of the function are just a block of code that you associate with the function, its scope is not the "object" part that you are referring to.
– TiiJ7
Feb 25 at 8:15
2
You've messed up something in your tests. It's perfectly legit and working to add a property to a function. For example, jQuery's$
is a function, but you can call some methods of it, like$.each()
.
– Teemu
Feb 25 at 8:21
2
Just to make it sure, see another fiddle = ).
– Teemu
Feb 25 at 8:36
|
show 17 more comments
"typeof Object" Of course, it is a function because this is constructor function. EvenNumber
, orBoolean
are functions.
– dfsq
Feb 25 at 7:33
2
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
Feb 25 at 7:46
2
You perfectly well can do eg.Foo.a = 5
. But be aware that this is independent of the variables you declare inside the function. The contents of the function are just a block of code that you associate with the function, its scope is not the "object" part that you are referring to.
– TiiJ7
Feb 25 at 8:15
2
You've messed up something in your tests. It's perfectly legit and working to add a property to a function. For example, jQuery's$
is a function, but you can call some methods of it, like$.each()
.
– Teemu
Feb 25 at 8:21
2
Just to make it sure, see another fiddle = ).
– Teemu
Feb 25 at 8:36
"typeof Object" Of course, it is a function because this is constructor function. Even
Number
, or Boolean
are functions.– dfsq
Feb 25 at 7:33
"typeof Object" Of course, it is a function because this is constructor function. Even
Number
, or Boolean
are functions.– dfsq
Feb 25 at 7:33
2
2
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
Feb 25 at 7:46
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
Feb 25 at 7:46
2
2
You perfectly well can do eg.
Foo.a = 5
. But be aware that this is independent of the variables you declare inside the function. The contents of the function are just a block of code that you associate with the function, its scope is not the "object" part that you are referring to.– TiiJ7
Feb 25 at 8:15
You perfectly well can do eg.
Foo.a = 5
. But be aware that this is independent of the variables you declare inside the function. The contents of the function are just a block of code that you associate with the function, its scope is not the "object" part that you are referring to.– TiiJ7
Feb 25 at 8:15
2
2
You've messed up something in your tests. It's perfectly legit and working to add a property to a function. For example, jQuery's
$
is a function, but you can call some methods of it, like $.each()
.– Teemu
Feb 25 at 8:21
You've messed up something in your tests. It's perfectly legit and working to add a property to a function. For example, jQuery's
$
is a function, but you can call some methods of it, like $.each()
.– Teemu
Feb 25 at 8:21
2
2
Just to make it sure, see another fiddle = ).
– Teemu
Feb 25 at 8:36
Just to make it sure, see another fiddle = ).
– Teemu
Feb 25 at 8:36
|
show 17 more comments
7 Answers
7
active
oldest
votes
You seem to be confused between "object" (the data structure) and Object
(the function).
An object is a concept in JavaScript that is a generic container for some data. An object contains properties with keys and associated values.
In JavaScript, everything that is not a primitive is an object. This includes functions, which are basically a special type of object that can be "called" with the ()
syntax.
JavaScript provides a number of built-in functions 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" (data structure).
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
A function that is meant to be called with the new
keyword is referred to as a "constructor function". Object
and Function
are both examples of constructor functions, which is why their names start with an uppercase letter (a convention in JavaScript).
When you create an object with a constructor function, the property prototype
of this function is used as the prototype (accessible through __proto__
) of the created object.
console.log(bar.constructor == Bar) // prints true
console.log(bar.__proto__ == Bar.prototype) // prints true
this
is also used for other things, but that is a broad subject and way out of scope for this question.
9
This answer is quite confusing because — I think — you’re usingcode formatting
to denote emphasis. Yet at the same time you want to explain (again, I think) the difference between concepts and specific JavaScript types. It would therefore be immensely helpful if you didn’t use backtick-fomatting for emphasis, but rather the proper emphasis, via*asterisks*
, to distinguish the two concepts typographically.
– Konrad Rudolph
Feb 25 at 15:57
I don't think there is aFunction
built-in to Javascript, just regular functions. This was hard to parse because of the code formatting
– TankorSmash
Feb 25 at 16:43
3
@TankorSmash - Yes, there is a built-in function calledFunction
in JavaScript (it creates functions from strings -- best avoided). There isn't anything built in calledobject
, which is part of what makes this answer confusing. (That and using "function
s" instead of "functions" when just using the word, not specifically referring to thefunction
keyword.)
– T.J. Crowder
Feb 25 at 17:43
3
@KonradRudolph Yeah, now that I read it again you are correct. I removed a lot of the unneeded backticks, I hope that makes it easier to read/understand. But if there is anything else I can work on, feel free to let me know. Most of my answers don't get much upvotes, so I guess I'm not used to the writing style.
– TiiJ7
Feb 25 at 18:29
2
@vikrantprototype
is just a property of the functionFunction
. And apparently it was defined as a function itself. Objects can have functions in their properties, and sinceFunction
is a function (and thus an object), it works for that too. I don't know why they decided to makeFunction.prototype
also a function, but it doesn't really matter since a function can do anything an object can.
– TiiJ7
Feb 25 at 19:01
|
show 2 more comments
Function and Object are both constructor functions which can be used to create a function and an object, respectively, which is the reason typeof Function
returns function
.
About how functions and objects are related in javascript, consider the following points:
- All non-primitive types are objects in JavaScript.
- All objects directly or indirectly inherit from Object.prototype (unless prototype is changed explicitly using setPrototypeOf).
- All native functions inherit from Function.prototype which inherits from Object.prototype, so it means function indirectly inherits from Object.prototype because functions are treated as objects in JavaScript.
- The reason functions are treated as objects is because they can be passed as parameters to other functions and can be returned from functions i.e. higher order functions(a powerful feature of javascript).
A function can be called using the
()
operator because the JavaScript engine knows it is declared using a function keyword and has executable code. So whenever it is called, the 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 they may have not been declared using the function keyword and not have executable code.
- As the function is treated as an object in JavaScript, we can add properties to it, create new objects from it.
- A non-function type object cannot be called using
()
because it does not have executable code and is not declared using the function keyword. Instead, it is declared usingnew Object()
or object notation and contains methods and properties.
I hope it clears both questions.
No. Not all objects inherit fromObject.prototype
, not all functions inherit fromFunction.prototype
. Functions are callable because that's their intrinsic feature (what makes these objects functions), not because they inherit fromFunction.prototype
. Similarly, objects can have properties because that's what defines an object, not because they inherit fromObject.prototype
. The inheritance chain (whose mere existence is another feature of being an object) only influences which inherited properties/methods are accessible, but does not otherwise affect the kind of value.
– Bergi
Feb 25 at 16:40
@Bergi What functions do not inherit from Function.prptotype? Also i said "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" not only because it inherits from Function.prototype.
– Nabil Shahid
Feb 25 at 17:31
var f = Object.setPrototypeOf(function() …, null); f()
for example. Yes, you didn't say "only because", but you said "because" - and the inheritance is not related at all to whether it can be called. "has executable code" is the only criterion.
– Bergi
Feb 25 at 18:05
@Bergi Thanks for the explanation. I have edited the answer accordingly.
– Nabil Shahid
Feb 25 at 19:04
add a comment |
Q1: Why are they pointing to Function?
A1: Because they are functions. Function
and Object
are just constructor functions.
function is a Function object
. object is an Object object
.
Q2: If everything is an object in JavaScript, 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 an instance of Function
, so that makes a function an object.
(function()) instanceof Function
// true
"And a function is a instance of Function, so that makes a function object." why does that make a function object?
– vikrant
Feb 25 at 8:00
What I was trying to say is just,Function
constructs an object which is function.
– zmag
Feb 25 at 8:09
if it is a function, why is it called object?
– vikrant
Feb 25 at 8:12
because it's an object ofFunction
. difference betweenFunction
andfunction
is a point.
– zmag
Feb 25 at 8:15
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
. That's why all classes like Object
and Functions
are functions
more precisely constructor functions
.
Answer 2
If everything is an object in JavaScript, then why is
Object
a function?
Same as Answer 1.
Also, how is a function actually implemented inside JavaScript?
Different JavaScript engines 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 the JavaScript compiler?
Not sure what are you asking.
1
Close this question if answered. Ask another question.this
keyword itself is a huge topic. medium.com/quick-code/…
– amit77309
Feb 25 at 9:03
add a comment |
The ECMAScript reference is very nice for answering such questions.
https://www.ecma-international.org/ecma-262/9.0/index.html#sec-ecmascript-language-types is the entry point about what types the language supports, and it introduces the concept that everything there is is either a rather standard value (boolean, string etc.), or an object. But no further types.
https://www.ecma-international.org/ecma-262/9.0/index.html#sec-object-type explains the Object Type. At heart, it's a collection of attributes and/or accessors (think getter/setter). This also introduces the term "function object".
https://www.ecma-international.org/ecma-262/9.0/index.html#sec-object-internal-methods-and-internal-slots shows the internal semantics, internal methods etc. of objects. At the end of that section, specifically, the "function object" specific internals ("Call" and "Construct") are described, including little details like the constructor being optional.
Hence, to answer your question: Foo
can be nothing but an object (because everything else on the menu is a basic value type). Hence, Foo()
is a special syntax which simply invokes the internal Call
method of that object. But Foo
itself is an object through and through, you can do with it everything else you can do with any other object, including setting arbitrary attributes on it.
Having a method defined on an object simply means that there is an attribute with that name, and the attribute references an object, which happens to be a "function object".
Something which can be used as a constructor is, again, simply an object which happens to have a Construct
internal method, and the syntactic sugar to call it.
As you know, there are no classes in JavaScript, it's a prototype-based object-oriented language (it's as object-oriented as you can get, it literally has nothing else). So any kind of "prototype" is just a link between objects. A constructor method (as explained in the link given above) simply calls the constructor method with the object on which it was called (i.e., something like String
) as an argument, just like the language would call Call
with this
being the object the method has been invoked on.
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 |
Yes, Object is of type function. It is a class/Function implementation which when invoked with new (new Object()
) will result in an object with allocated memory.
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
7 Answers
7
active
oldest
votes
7 Answers
7
active
oldest
votes
active
oldest
votes
active
oldest
votes
You seem to be confused between "object" (the data structure) and Object
(the function).
An object is a concept in JavaScript that is a generic container for some data. An object contains properties with keys and associated values.
In JavaScript, everything that is not a primitive is an object. This includes functions, which are basically a special type of object that can be "called" with the ()
syntax.
JavaScript provides a number of built-in functions 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" (data structure).
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
A function that is meant to be called with the new
keyword is referred to as a "constructor function". Object
and Function
are both examples of constructor functions, which is why their names start with an uppercase letter (a convention in JavaScript).
When you create an object with a constructor function, the property prototype
of this function is used as the prototype (accessible through __proto__
) of the created object.
console.log(bar.constructor == Bar) // prints true
console.log(bar.__proto__ == Bar.prototype) // prints true
this
is also used for other things, but that is a broad subject and way out of scope for this question.
9
This answer is quite confusing because — I think — you’re usingcode formatting
to denote emphasis. Yet at the same time you want to explain (again, I think) the difference between concepts and specific JavaScript types. It would therefore be immensely helpful if you didn’t use backtick-fomatting for emphasis, but rather the proper emphasis, via*asterisks*
, to distinguish the two concepts typographically.
– Konrad Rudolph
Feb 25 at 15:57
I don't think there is aFunction
built-in to Javascript, just regular functions. This was hard to parse because of the code formatting
– TankorSmash
Feb 25 at 16:43
3
@TankorSmash - Yes, there is a built-in function calledFunction
in JavaScript (it creates functions from strings -- best avoided). There isn't anything built in calledobject
, which is part of what makes this answer confusing. (That and using "function
s" instead of "functions" when just using the word, not specifically referring to thefunction
keyword.)
– T.J. Crowder
Feb 25 at 17:43
3
@KonradRudolph Yeah, now that I read it again you are correct. I removed a lot of the unneeded backticks, I hope that makes it easier to read/understand. But if there is anything else I can work on, feel free to let me know. Most of my answers don't get much upvotes, so I guess I'm not used to the writing style.
– TiiJ7
Feb 25 at 18:29
2
@vikrantprototype
is just a property of the functionFunction
. And apparently it was defined as a function itself. Objects can have functions in their properties, and sinceFunction
is a function (and thus an object), it works for that too. I don't know why they decided to makeFunction.prototype
also a function, but it doesn't really matter since a function can do anything an object can.
– TiiJ7
Feb 25 at 19:01
|
show 2 more comments
You seem to be confused between "object" (the data structure) and Object
(the function).
An object is a concept in JavaScript that is a generic container for some data. An object contains properties with keys and associated values.
In JavaScript, everything that is not a primitive is an object. This includes functions, which are basically a special type of object that can be "called" with the ()
syntax.
JavaScript provides a number of built-in functions 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" (data structure).
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
A function that is meant to be called with the new
keyword is referred to as a "constructor function". Object
and Function
are both examples of constructor functions, which is why their names start with an uppercase letter (a convention in JavaScript).
When you create an object with a constructor function, the property prototype
of this function is used as the prototype (accessible through __proto__
) of the created object.
console.log(bar.constructor == Bar) // prints true
console.log(bar.__proto__ == Bar.prototype) // prints true
this
is also used for other things, but that is a broad subject and way out of scope for this question.
9
This answer is quite confusing because — I think — you’re usingcode formatting
to denote emphasis. Yet at the same time you want to explain (again, I think) the difference between concepts and specific JavaScript types. It would therefore be immensely helpful if you didn’t use backtick-fomatting for emphasis, but rather the proper emphasis, via*asterisks*
, to distinguish the two concepts typographically.
– Konrad Rudolph
Feb 25 at 15:57
I don't think there is aFunction
built-in to Javascript, just regular functions. This was hard to parse because of the code formatting
– TankorSmash
Feb 25 at 16:43
3
@TankorSmash - Yes, there is a built-in function calledFunction
in JavaScript (it creates functions from strings -- best avoided). There isn't anything built in calledobject
, which is part of what makes this answer confusing. (That and using "function
s" instead of "functions" when just using the word, not specifically referring to thefunction
keyword.)
– T.J. Crowder
Feb 25 at 17:43
3
@KonradRudolph Yeah, now that I read it again you are correct. I removed a lot of the unneeded backticks, I hope that makes it easier to read/understand. But if there is anything else I can work on, feel free to let me know. Most of my answers don't get much upvotes, so I guess I'm not used to the writing style.
– TiiJ7
Feb 25 at 18:29
2
@vikrantprototype
is just a property of the functionFunction
. And apparently it was defined as a function itself. Objects can have functions in their properties, and sinceFunction
is a function (and thus an object), it works for that too. I don't know why they decided to makeFunction.prototype
also a function, but it doesn't really matter since a function can do anything an object can.
– TiiJ7
Feb 25 at 19:01
|
show 2 more comments
You seem to be confused between "object" (the data structure) and Object
(the function).
An object is a concept in JavaScript that is a generic container for some data. An object contains properties with keys and associated values.
In JavaScript, everything that is not a primitive is an object. This includes functions, which are basically a special type of object that can be "called" with the ()
syntax.
JavaScript provides a number of built-in functions 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" (data structure).
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
A function that is meant to be called with the new
keyword is referred to as a "constructor function". Object
and Function
are both examples of constructor functions, which is why their names start with an uppercase letter (a convention in JavaScript).
When you create an object with a constructor function, the property prototype
of this function is used as the prototype (accessible through __proto__
) of the created object.
console.log(bar.constructor == Bar) // prints true
console.log(bar.__proto__ == Bar.prototype) // prints true
this
is also used for other things, but that is a broad subject and way out of scope for this question.
You seem to be confused between "object" (the data structure) and Object
(the function).
An object is a concept in JavaScript that is a generic container for some data. An object contains properties with keys and associated values.
In JavaScript, everything that is not a primitive is an object. This includes functions, which are basically a special type of object that can be "called" with the ()
syntax.
JavaScript provides a number of built-in functions 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" (data structure).
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
A function that is meant to be called with the new
keyword is referred to as a "constructor function". Object
and Function
are both examples of constructor functions, which is why their names start with an uppercase letter (a convention in JavaScript).
When you create an object with a constructor function, the property prototype
of this function is used as the prototype (accessible through __proto__
) of the created object.
console.log(bar.constructor == Bar) // prints true
console.log(bar.__proto__ == Bar.prototype) // prints true
this
is also used for other things, but that is a broad subject and way out of scope for this question.
edited Feb 25 at 18:25
answered Feb 25 at 8:52
TiiJ7TiiJ7
2,47711728
2,47711728
9
This answer is quite confusing because — I think — you’re usingcode formatting
to denote emphasis. Yet at the same time you want to explain (again, I think) the difference between concepts and specific JavaScript types. It would therefore be immensely helpful if you didn’t use backtick-fomatting for emphasis, but rather the proper emphasis, via*asterisks*
, to distinguish the two concepts typographically.
– Konrad Rudolph
Feb 25 at 15:57
I don't think there is aFunction
built-in to Javascript, just regular functions. This was hard to parse because of the code formatting
– TankorSmash
Feb 25 at 16:43
3
@TankorSmash - Yes, there is a built-in function calledFunction
in JavaScript (it creates functions from strings -- best avoided). There isn't anything built in calledobject
, which is part of what makes this answer confusing. (That and using "function
s" instead of "functions" when just using the word, not specifically referring to thefunction
keyword.)
– T.J. Crowder
Feb 25 at 17:43
3
@KonradRudolph Yeah, now that I read it again you are correct. I removed a lot of the unneeded backticks, I hope that makes it easier to read/understand. But if there is anything else I can work on, feel free to let me know. Most of my answers don't get much upvotes, so I guess I'm not used to the writing style.
– TiiJ7
Feb 25 at 18:29
2
@vikrantprototype
is just a property of the functionFunction
. And apparently it was defined as a function itself. Objects can have functions in their properties, and sinceFunction
is a function (and thus an object), it works for that too. I don't know why they decided to makeFunction.prototype
also a function, but it doesn't really matter since a function can do anything an object can.
– TiiJ7
Feb 25 at 19:01
|
show 2 more comments
9
This answer is quite confusing because — I think — you’re usingcode formatting
to denote emphasis. Yet at the same time you want to explain (again, I think) the difference between concepts and specific JavaScript types. It would therefore be immensely helpful if you didn’t use backtick-fomatting for emphasis, but rather the proper emphasis, via*asterisks*
, to distinguish the two concepts typographically.
– Konrad Rudolph
Feb 25 at 15:57
I don't think there is aFunction
built-in to Javascript, just regular functions. This was hard to parse because of the code formatting
– TankorSmash
Feb 25 at 16:43
3
@TankorSmash - Yes, there is a built-in function calledFunction
in JavaScript (it creates functions from strings -- best avoided). There isn't anything built in calledobject
, which is part of what makes this answer confusing. (That and using "function
s" instead of "functions" when just using the word, not specifically referring to thefunction
keyword.)
– T.J. Crowder
Feb 25 at 17:43
3
@KonradRudolph Yeah, now that I read it again you are correct. I removed a lot of the unneeded backticks, I hope that makes it easier to read/understand. But if there is anything else I can work on, feel free to let me know. Most of my answers don't get much upvotes, so I guess I'm not used to the writing style.
– TiiJ7
Feb 25 at 18:29
2
@vikrantprototype
is just a property of the functionFunction
. And apparently it was defined as a function itself. Objects can have functions in their properties, and sinceFunction
is a function (and thus an object), it works for that too. I don't know why they decided to makeFunction.prototype
also a function, but it doesn't really matter since a function can do anything an object can.
– TiiJ7
Feb 25 at 19:01
9
9
This answer is quite confusing because — I think — you’re using
code formatting
to denote emphasis. Yet at the same time you want to explain (again, I think) the difference between concepts and specific JavaScript types. It would therefore be immensely helpful if you didn’t use backtick-fomatting for emphasis, but rather the proper emphasis, via *asterisks*
, to distinguish the two concepts typographically.– Konrad Rudolph
Feb 25 at 15:57
This answer is quite confusing because — I think — you’re using
code formatting
to denote emphasis. Yet at the same time you want to explain (again, I think) the difference between concepts and specific JavaScript types. It would therefore be immensely helpful if you didn’t use backtick-fomatting for emphasis, but rather the proper emphasis, via *asterisks*
, to distinguish the two concepts typographically.– Konrad Rudolph
Feb 25 at 15:57
I don't think there is a
Function
built-in to Javascript, just regular functions. This was hard to parse because of the code formatting– TankorSmash
Feb 25 at 16:43
I don't think there is a
Function
built-in to Javascript, just regular functions. This was hard to parse because of the code formatting– TankorSmash
Feb 25 at 16:43
3
3
@TankorSmash - Yes, there is a built-in function called
Function
in JavaScript (it creates functions from strings -- best avoided). There isn't anything built in called object
, which is part of what makes this answer confusing. (That and using "function
s" instead of "functions" when just using the word, not specifically referring to the function
keyword.)– T.J. Crowder
Feb 25 at 17:43
@TankorSmash - Yes, there is a built-in function called
Function
in JavaScript (it creates functions from strings -- best avoided). There isn't anything built in called object
, which is part of what makes this answer confusing. (That and using "function
s" instead of "functions" when just using the word, not specifically referring to the function
keyword.)– T.J. Crowder
Feb 25 at 17:43
3
3
@KonradRudolph Yeah, now that I read it again you are correct. I removed a lot of the unneeded backticks, I hope that makes it easier to read/understand. But if there is anything else I can work on, feel free to let me know. Most of my answers don't get much upvotes, so I guess I'm not used to the writing style.
– TiiJ7
Feb 25 at 18:29
@KonradRudolph Yeah, now that I read it again you are correct. I removed a lot of the unneeded backticks, I hope that makes it easier to read/understand. But if there is anything else I can work on, feel free to let me know. Most of my answers don't get much upvotes, so I guess I'm not used to the writing style.
– TiiJ7
Feb 25 at 18:29
2
2
@vikrant
prototype
is just a property of the function Function
. And apparently it was defined as a function itself. Objects can have functions in their properties, and since Function
is a function (and thus an object), it works for that too. I don't know why they decided to make Function.prototype
also a function, but it doesn't really matter since a function can do anything an object can.– TiiJ7
Feb 25 at 19:01
@vikrant
prototype
is just a property of the function Function
. And apparently it was defined as a function itself. Objects can have functions in their properties, and since Function
is a function (and thus an object), it works for that too. I don't know why they decided to make Function.prototype
also a function, but it doesn't really matter since a function can do anything an object can.– TiiJ7
Feb 25 at 19:01
|
show 2 more comments
Function and Object are both constructor functions which can be used to create a function and an object, respectively, which is the reason typeof Function
returns function
.
About how functions and objects are related in javascript, consider the following points:
- All non-primitive types are objects in JavaScript.
- All objects directly or indirectly inherit from Object.prototype (unless prototype is changed explicitly using setPrototypeOf).
- All native functions inherit from Function.prototype which inherits from Object.prototype, so it means function indirectly inherits from Object.prototype because functions are treated as objects in JavaScript.
- The reason functions are treated as objects is because they can be passed as parameters to other functions and can be returned from functions i.e. higher order functions(a powerful feature of javascript).
A function can be called using the
()
operator because the JavaScript engine knows it is declared using a function keyword and has executable code. So whenever it is called, the 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 they may have not been declared using the function keyword and not have executable code.
- As the function is treated as an object in JavaScript, we can add properties to it, create new objects from it.
- A non-function type object cannot be called using
()
because it does not have executable code and is not declared using the function keyword. Instead, it is declared usingnew Object()
or object notation and contains methods and properties.
I hope it clears both questions.
No. Not all objects inherit fromObject.prototype
, not all functions inherit fromFunction.prototype
. Functions are callable because that's their intrinsic feature (what makes these objects functions), not because they inherit fromFunction.prototype
. Similarly, objects can have properties because that's what defines an object, not because they inherit fromObject.prototype
. The inheritance chain (whose mere existence is another feature of being an object) only influences which inherited properties/methods are accessible, but does not otherwise affect the kind of value.
– Bergi
Feb 25 at 16:40
@Bergi What functions do not inherit from Function.prptotype? Also i said "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" not only because it inherits from Function.prototype.
– Nabil Shahid
Feb 25 at 17:31
var f = Object.setPrototypeOf(function() …, null); f()
for example. Yes, you didn't say "only because", but you said "because" - and the inheritance is not related at all to whether it can be called. "has executable code" is the only criterion.
– Bergi
Feb 25 at 18:05
@Bergi Thanks for the explanation. I have edited the answer accordingly.
– Nabil Shahid
Feb 25 at 19:04
add a comment |
Function and Object are both constructor functions which can be used to create a function and an object, respectively, which is the reason typeof Function
returns function
.
About how functions and objects are related in javascript, consider the following points:
- All non-primitive types are objects in JavaScript.
- All objects directly or indirectly inherit from Object.prototype (unless prototype is changed explicitly using setPrototypeOf).
- All native functions inherit from Function.prototype which inherits from Object.prototype, so it means function indirectly inherits from Object.prototype because functions are treated as objects in JavaScript.
- The reason functions are treated as objects is because they can be passed as parameters to other functions and can be returned from functions i.e. higher order functions(a powerful feature of javascript).
A function can be called using the
()
operator because the JavaScript engine knows it is declared using a function keyword and has executable code. So whenever it is called, the 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 they may have not been declared using the function keyword and not have executable code.
- As the function is treated as an object in JavaScript, we can add properties to it, create new objects from it.
- A non-function type object cannot be called using
()
because it does not have executable code and is not declared using the function keyword. Instead, it is declared usingnew Object()
or object notation and contains methods and properties.
I hope it clears both questions.
No. Not all objects inherit fromObject.prototype
, not all functions inherit fromFunction.prototype
. Functions are callable because that's their intrinsic feature (what makes these objects functions), not because they inherit fromFunction.prototype
. Similarly, objects can have properties because that's what defines an object, not because they inherit fromObject.prototype
. The inheritance chain (whose mere existence is another feature of being an object) only influences which inherited properties/methods are accessible, but does not otherwise affect the kind of value.
– Bergi
Feb 25 at 16:40
@Bergi What functions do not inherit from Function.prptotype? Also i said "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" not only because it inherits from Function.prototype.
– Nabil Shahid
Feb 25 at 17:31
var f = Object.setPrototypeOf(function() …, null); f()
for example. Yes, you didn't say "only because", but you said "because" - and the inheritance is not related at all to whether it can be called. "has executable code" is the only criterion.
– Bergi
Feb 25 at 18:05
@Bergi Thanks for the explanation. I have edited the answer accordingly.
– Nabil Shahid
Feb 25 at 19:04
add a comment |
Function and Object are both constructor functions which can be used to create a function and an object, respectively, which is the reason typeof Function
returns function
.
About how functions and objects are related in javascript, consider the following points:
- All non-primitive types are objects in JavaScript.
- All objects directly or indirectly inherit from Object.prototype (unless prototype is changed explicitly using setPrototypeOf).
- All native functions inherit from Function.prototype which inherits from Object.prototype, so it means function indirectly inherits from Object.prototype because functions are treated as objects in JavaScript.
- The reason functions are treated as objects is because they can be passed as parameters to other functions and can be returned from functions i.e. higher order functions(a powerful feature of javascript).
A function can be called using the
()
operator because the JavaScript engine knows it is declared using a function keyword and has executable code. So whenever it is called, the 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 they may have not been declared using the function keyword and not have executable code.
- As the function is treated as an object in JavaScript, we can add properties to it, create new objects from it.
- A non-function type object cannot be called using
()
because it does not have executable code and is not declared using the function keyword. Instead, it is declared usingnew Object()
or object notation and contains methods and properties.
I hope it clears both questions.
Function and Object are both constructor functions which can be used to create a function and an object, respectively, which is the reason typeof Function
returns function
.
About how functions and objects are related in javascript, consider the following points:
- All non-primitive types are objects in JavaScript.
- All objects directly or indirectly inherit from Object.prototype (unless prototype is changed explicitly using setPrototypeOf).
- All native functions inherit from Function.prototype which inherits from Object.prototype, so it means function indirectly inherits from Object.prototype because functions are treated as objects in JavaScript.
- The reason functions are treated as objects is because they can be passed as parameters to other functions and can be returned from functions i.e. higher order functions(a powerful feature of javascript).
A function can be called using the
()
operator because the JavaScript engine knows it is declared using a function keyword and has executable code. So whenever it is called, the 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 they may have not been declared using the function keyword and not have executable code.
- As the function is treated as an object in JavaScript, we can add properties to it, create new objects from it.
- A non-function type object cannot be called using
()
because it does not have executable code and is not declared using the function keyword. Instead, it is declared usingnew Object()
or object notation and contains methods and properties.
I hope it clears both questions.
edited Mar 19 at 5:37
answered Feb 25 at 8:37
Nabil ShahidNabil Shahid
81839
81839
No. Not all objects inherit fromObject.prototype
, not all functions inherit fromFunction.prototype
. Functions are callable because that's their intrinsic feature (what makes these objects functions), not because they inherit fromFunction.prototype
. Similarly, objects can have properties because that's what defines an object, not because they inherit fromObject.prototype
. The inheritance chain (whose mere existence is another feature of being an object) only influences which inherited properties/methods are accessible, but does not otherwise affect the kind of value.
– Bergi
Feb 25 at 16:40
@Bergi What functions do not inherit from Function.prptotype? Also i said "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" not only because it inherits from Function.prototype.
– Nabil Shahid
Feb 25 at 17:31
var f = Object.setPrototypeOf(function() …, null); f()
for example. Yes, you didn't say "only because", but you said "because" - and the inheritance is not related at all to whether it can be called. "has executable code" is the only criterion.
– Bergi
Feb 25 at 18:05
@Bergi Thanks for the explanation. I have edited the answer accordingly.
– Nabil Shahid
Feb 25 at 19:04
add a comment |
No. Not all objects inherit fromObject.prototype
, not all functions inherit fromFunction.prototype
. Functions are callable because that's their intrinsic feature (what makes these objects functions), not because they inherit fromFunction.prototype
. Similarly, objects can have properties because that's what defines an object, not because they inherit fromObject.prototype
. The inheritance chain (whose mere existence is another feature of being an object) only influences which inherited properties/methods are accessible, but does not otherwise affect the kind of value.
– Bergi
Feb 25 at 16:40
@Bergi What functions do not inherit from Function.prptotype? Also i said "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" not only because it inherits from Function.prototype.
– Nabil Shahid
Feb 25 at 17:31
var f = Object.setPrototypeOf(function() …, null); f()
for example. Yes, you didn't say "only because", but you said "because" - and the inheritance is not related at all to whether it can be called. "has executable code" is the only criterion.
– Bergi
Feb 25 at 18:05
@Bergi Thanks for the explanation. I have edited the answer accordingly.
– Nabil Shahid
Feb 25 at 19:04
No. Not all objects inherit from
Object.prototype
, not all functions inherit from Function.prototype
. Functions are callable because that's their intrinsic feature (what makes these objects functions), not because they inherit from Function.prototype
. Similarly, objects can have properties because that's what defines an object, not because they inherit from Object.prototype
. The inheritance chain (whose mere existence is another feature of being an object) only influences which inherited properties/methods are accessible, but does not otherwise affect the kind of value.– Bergi
Feb 25 at 16:40
No. Not all objects inherit from
Object.prototype
, not all functions inherit from Function.prototype
. Functions are callable because that's their intrinsic feature (what makes these objects functions), not because they inherit from Function.prototype
. Similarly, objects can have properties because that's what defines an object, not because they inherit from Object.prototype
. The inheritance chain (whose mere existence is another feature of being an object) only influences which inherited properties/methods are accessible, but does not otherwise affect the kind of value.– Bergi
Feb 25 at 16:40
@Bergi What functions do not inherit from Function.prptotype? Also i said "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" not only because it inherits from Function.prototype.
– Nabil Shahid
Feb 25 at 17:31
@Bergi What functions do not inherit from Function.prptotype? Also i said "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" not only because it inherits from Function.prototype.
– Nabil Shahid
Feb 25 at 17:31
var f = Object.setPrototypeOf(function() …, null); f()
for example. Yes, you didn't say "only because", but you said "because" - and the inheritance is not related at all to whether it can be called. "has executable code" is the only criterion.– Bergi
Feb 25 at 18:05
var f = Object.setPrototypeOf(function() …, null); f()
for example. Yes, you didn't say "only because", but you said "because" - and the inheritance is not related at all to whether it can be called. "has executable code" is the only criterion.– Bergi
Feb 25 at 18:05
@Bergi Thanks for the explanation. I have edited the answer accordingly.
– Nabil Shahid
Feb 25 at 19:04
@Bergi Thanks for the explanation. I have edited the answer accordingly.
– Nabil Shahid
Feb 25 at 19:04
add a comment |
Q1: Why are they pointing to Function?
A1: Because they are functions. Function
and Object
are just constructor functions.
function is a Function object
. object is an Object object
.
Q2: If everything is an object in JavaScript, 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 an instance of Function
, so that makes a function an object.
(function()) instanceof Function
// true
"And a function is a instance of Function, so that makes a function object." why does that make a function object?
– vikrant
Feb 25 at 8:00
What I was trying to say is just,Function
constructs an object which is function.
– zmag
Feb 25 at 8:09
if it is a function, why is it called object?
– vikrant
Feb 25 at 8:12
because it's an object ofFunction
. difference betweenFunction
andfunction
is a point.
– zmag
Feb 25 at 8:15
add a comment |
Q1: Why are they pointing to Function?
A1: Because they are functions. Function
and Object
are just constructor functions.
function is a Function object
. object is an Object object
.
Q2: If everything is an object in JavaScript, 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 an instance of Function
, so that makes a function an object.
(function()) instanceof Function
// true
"And a function is a instance of Function, so that makes a function object." why does that make a function object?
– vikrant
Feb 25 at 8:00
What I was trying to say is just,Function
constructs an object which is function.
– zmag
Feb 25 at 8:09
if it is a function, why is it called object?
– vikrant
Feb 25 at 8:12
because it's an object ofFunction
. difference betweenFunction
andfunction
is a point.
– zmag
Feb 25 at 8:15
add a comment |
Q1: Why are they pointing to Function?
A1: Because they are functions. Function
and Object
are just constructor functions.
function is a Function object
. object is an Object object
.
Q2: If everything is an object in JavaScript, 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 an instance of Function
, so that makes a function an object.
(function()) instanceof Function
// true
Q1: Why are they pointing to Function?
A1: Because they are functions. Function
and Object
are just constructor functions.
function is a Function object
. object is an Object object
.
Q2: If everything is an object in JavaScript, 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 an instance of Function
, so that makes a function an object.
(function()) instanceof Function
// true
edited Feb 26 at 8:32
answered Feb 25 at 7:49
zmagzmag
2,23811125
2,23811125
"And a function is a instance of Function, so that makes a function object." why does that make a function object?
– vikrant
Feb 25 at 8:00
What I was trying to say is just,Function
constructs an object which is function.
– zmag
Feb 25 at 8:09
if it is a function, why is it called object?
– vikrant
Feb 25 at 8:12
because it's an object ofFunction
. difference betweenFunction
andfunction
is a point.
– zmag
Feb 25 at 8:15
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
Feb 25 at 8:00
What I was trying to say is just,Function
constructs an object which is function.
– zmag
Feb 25 at 8:09
if it is a function, why is it called object?
– vikrant
Feb 25 at 8:12
because it's an object ofFunction
. difference betweenFunction
andfunction
is a point.
– zmag
Feb 25 at 8:15
"And a function is a instance of Function, so that makes a function object." why does that make a function object?
– vikrant
Feb 25 at 8:00
"And a function is a instance of Function, so that makes a function object." why does that make a function object?
– vikrant
Feb 25 at 8:00
What I was trying to say is just,
Function
constructs an object which is function.– zmag
Feb 25 at 8:09
What I was trying to say is just,
Function
constructs an object which is function.– zmag
Feb 25 at 8:09
if it is a function, why is it called object?
– vikrant
Feb 25 at 8:12
if it is a function, why is it called object?
– vikrant
Feb 25 at 8:12
because it's an object of
Function
. difference between Function
and function
is a point.– zmag
Feb 25 at 8:15
because it's an object of
Function
. difference between Function
and function
is a point.– zmag
Feb 25 at 8:15
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
. That's why all classes like Object
and Functions
are functions
more precisely constructor functions
.
Answer 2
If everything is an object in JavaScript, then why is
Object
a function?
Same as Answer 1.
Also, how is a function actually implemented inside JavaScript?
Different JavaScript engines 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 the JavaScript compiler?
Not sure what are you asking.
1
Close this question if answered. Ask another question.this
keyword itself is a huge topic. medium.com/quick-code/…
– amit77309
Feb 25 at 9:03
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
. That's why all classes like Object
and Functions
are functions
more precisely constructor functions
.
Answer 2
If everything is an object in JavaScript, then why is
Object
a function?
Same as Answer 1.
Also, how is a function actually implemented inside JavaScript?
Different JavaScript engines 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 the JavaScript compiler?
Not sure what are you asking.
1
Close this question if answered. Ask another question.this
keyword itself is a huge topic. medium.com/quick-code/…
– amit77309
Feb 25 at 9:03
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
. That's why all classes like Object
and Functions
are functions
more precisely constructor functions
.
Answer 2
If everything is an object in JavaScript, then why is
Object
a function?
Same as Answer 1.
Also, how is a function actually implemented inside JavaScript?
Different JavaScript engines 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 the JavaScript 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
. That's why all classes like Object
and Functions
are functions
more precisely constructor functions
.
Answer 2
If everything is an object in JavaScript, then why is
Object
a function?
Same as Answer 1.
Also, how is a function actually implemented inside JavaScript?
Different JavaScript engines 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 the JavaScript compiler?
Not sure what are you asking.
edited Feb 26 at 8:04
Peter Mortensen
13.8k1987113
13.8k1987113
answered Feb 25 at 8:08
amit77309amit77309
3,88631119
3,88631119
1
Close this question if answered. Ask another question.this
keyword itself is a huge topic. medium.com/quick-code/…
– amit77309
Feb 25 at 9:03
add a comment |
1
Close this question if answered. Ask another question.this
keyword itself is a huge topic. medium.com/quick-code/…
– amit77309
Feb 25 at 9:03
1
1
Close this question if answered. Ask another question.
this
keyword itself is a huge topic. medium.com/quick-code/…– amit77309
Feb 25 at 9:03
Close this question if answered. Ask another question.
this
keyword itself is a huge topic. medium.com/quick-code/…– amit77309
Feb 25 at 9:03
add a comment |
The ECMAScript reference is very nice for answering such questions.
https://www.ecma-international.org/ecma-262/9.0/index.html#sec-ecmascript-language-types is the entry point about what types the language supports, and it introduces the concept that everything there is is either a rather standard value (boolean, string etc.), or an object. But no further types.
https://www.ecma-international.org/ecma-262/9.0/index.html#sec-object-type explains the Object Type. At heart, it's a collection of attributes and/or accessors (think getter/setter). This also introduces the term "function object".
https://www.ecma-international.org/ecma-262/9.0/index.html#sec-object-internal-methods-and-internal-slots shows the internal semantics, internal methods etc. of objects. At the end of that section, specifically, the "function object" specific internals ("Call" and "Construct") are described, including little details like the constructor being optional.
Hence, to answer your question: Foo
can be nothing but an object (because everything else on the menu is a basic value type). Hence, Foo()
is a special syntax which simply invokes the internal Call
method of that object. But Foo
itself is an object through and through, you can do with it everything else you can do with any other object, including setting arbitrary attributes on it.
Having a method defined on an object simply means that there is an attribute with that name, and the attribute references an object, which happens to be a "function object".
Something which can be used as a constructor is, again, simply an object which happens to have a Construct
internal method, and the syntactic sugar to call it.
As you know, there are no classes in JavaScript, it's a prototype-based object-oriented language (it's as object-oriented as you can get, it literally has nothing else). So any kind of "prototype" is just a link between objects. A constructor method (as explained in the link given above) simply calls the constructor method with the object on which it was called (i.e., something like String
) as an argument, just like the language would call Call
with this
being the object the method has been invoked on.
add a comment |
The ECMAScript reference is very nice for answering such questions.
https://www.ecma-international.org/ecma-262/9.0/index.html#sec-ecmascript-language-types is the entry point about what types the language supports, and it introduces the concept that everything there is is either a rather standard value (boolean, string etc.), or an object. But no further types.
https://www.ecma-international.org/ecma-262/9.0/index.html#sec-object-type explains the Object Type. At heart, it's a collection of attributes and/or accessors (think getter/setter). This also introduces the term "function object".
https://www.ecma-international.org/ecma-262/9.0/index.html#sec-object-internal-methods-and-internal-slots shows the internal semantics, internal methods etc. of objects. At the end of that section, specifically, the "function object" specific internals ("Call" and "Construct") are described, including little details like the constructor being optional.
Hence, to answer your question: Foo
can be nothing but an object (because everything else on the menu is a basic value type). Hence, Foo()
is a special syntax which simply invokes the internal Call
method of that object. But Foo
itself is an object through and through, you can do with it everything else you can do with any other object, including setting arbitrary attributes on it.
Having a method defined on an object simply means that there is an attribute with that name, and the attribute references an object, which happens to be a "function object".
Something which can be used as a constructor is, again, simply an object which happens to have a Construct
internal method, and the syntactic sugar to call it.
As you know, there are no classes in JavaScript, it's a prototype-based object-oriented language (it's as object-oriented as you can get, it literally has nothing else). So any kind of "prototype" is just a link between objects. A constructor method (as explained in the link given above) simply calls the constructor method with the object on which it was called (i.e., something like String
) as an argument, just like the language would call Call
with this
being the object the method has been invoked on.
add a comment |
The ECMAScript reference is very nice for answering such questions.
https://www.ecma-international.org/ecma-262/9.0/index.html#sec-ecmascript-language-types is the entry point about what types the language supports, and it introduces the concept that everything there is is either a rather standard value (boolean, string etc.), or an object. But no further types.
https://www.ecma-international.org/ecma-262/9.0/index.html#sec-object-type explains the Object Type. At heart, it's a collection of attributes and/or accessors (think getter/setter). This also introduces the term "function object".
https://www.ecma-international.org/ecma-262/9.0/index.html#sec-object-internal-methods-and-internal-slots shows the internal semantics, internal methods etc. of objects. At the end of that section, specifically, the "function object" specific internals ("Call" and "Construct") are described, including little details like the constructor being optional.
Hence, to answer your question: Foo
can be nothing but an object (because everything else on the menu is a basic value type). Hence, Foo()
is a special syntax which simply invokes the internal Call
method of that object. But Foo
itself is an object through and through, you can do with it everything else you can do with any other object, including setting arbitrary attributes on it.
Having a method defined on an object simply means that there is an attribute with that name, and the attribute references an object, which happens to be a "function object".
Something which can be used as a constructor is, again, simply an object which happens to have a Construct
internal method, and the syntactic sugar to call it.
As you know, there are no classes in JavaScript, it's a prototype-based object-oriented language (it's as object-oriented as you can get, it literally has nothing else). So any kind of "prototype" is just a link between objects. A constructor method (as explained in the link given above) simply calls the constructor method with the object on which it was called (i.e., something like String
) as an argument, just like the language would call Call
with this
being the object the method has been invoked on.
The ECMAScript reference is very nice for answering such questions.
https://www.ecma-international.org/ecma-262/9.0/index.html#sec-ecmascript-language-types is the entry point about what types the language supports, and it introduces the concept that everything there is is either a rather standard value (boolean, string etc.), or an object. But no further types.
https://www.ecma-international.org/ecma-262/9.0/index.html#sec-object-type explains the Object Type. At heart, it's a collection of attributes and/or accessors (think getter/setter). This also introduces the term "function object".
https://www.ecma-international.org/ecma-262/9.0/index.html#sec-object-internal-methods-and-internal-slots shows the internal semantics, internal methods etc. of objects. At the end of that section, specifically, the "function object" specific internals ("Call" and "Construct") are described, including little details like the constructor being optional.
Hence, to answer your question: Foo
can be nothing but an object (because everything else on the menu is a basic value type). Hence, Foo()
is a special syntax which simply invokes the internal Call
method of that object. But Foo
itself is an object through and through, you can do with it everything else you can do with any other object, including setting arbitrary attributes on it.
Having a method defined on an object simply means that there is an attribute with that name, and the attribute references an object, which happens to be a "function object".
Something which can be used as a constructor is, again, simply an object which happens to have a Construct
internal method, and the syntactic sugar to call it.
As you know, there are no classes in JavaScript, it's a prototype-based object-oriented language (it's as object-oriented as you can get, it literally has nothing else). So any kind of "prototype" is just a link between objects. A constructor method (as explained in the link given above) simply calls the constructor method with the object on which it was called (i.e., something like String
) as an argument, just like the language would call Call
with this
being the object the method has been invoked on.
answered Feb 26 at 10:35
AnoEAnoE
5,7341921
5,7341921
add a comment |
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 Feb 25 at 8:01
FZsFZs
2,25631225
2,25631225
add a comment |
add a comment |
Yes, Object is of type function. It is a class/Function implementation which when invoked with new (new Object()
) will result in an object with allocated memory.
add a comment |
Yes, Object is of type function. It is a class/Function implementation which when invoked with new (new Object()
) will result in an object with allocated memory.
add a comment |
Yes, Object is of type function. It is a class/Function implementation which when invoked with new (new Object()
) will result in an object with allocated memory.
Yes, Object is of type function. It is a class/Function implementation which when invoked with new (new Object()
) will result in an object with allocated memory.
edited Feb 26 at 8:08
Peter Mortensen
13.8k1987113
13.8k1987113
answered Feb 25 at 18:54
Rahul KandariRahul Kandari
311
311
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
Feb 25 at 7:33
2
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
Feb 25 at 7:46
2
You perfectly well can do eg.
Foo.a = 5
. But be aware that this is independent of the variables you declare inside the function. The contents of the function are just a block of code that you associate with the function, its scope is not the "object" part that you are referring to.– TiiJ7
Feb 25 at 8:15
2
You've messed up something in your tests. It's perfectly legit and working to add a property to a function. For example, jQuery's
$
is a function, but you can call some methods of it, like$.each()
.– Teemu
Feb 25 at 8:21
2
Just to make it sure, see another fiddle = ).
– Teemu
Feb 25 at 8:36