Functions in Javascript are objects. And the class keyword in ES6 is a syntactical sugar and actually is a function, so classes are objects?
-
What's the actual question/thought behind this question? If it's about inheritance, almost everything in JS inherits from Object, even primitives, although they ain't instances of Object. Or is it about type checking? Or what else is the question about?Thomas– Thomas2017-04-22 13:21:30 +00:00Commented Apr 22, 2017 at 13:21
-
I have a class based OOP background with a static language. This seems really weird to me. When I create an object from a class (eg. MyClass) and check it with typeof, also typeof says it's 'object', not 'MyClass'.Umut Özdemir– Umut Özdemir2017-04-22 21:32:50 +00:00Commented Apr 22, 2017 at 21:32
Add a comment
|
1 Answer
Yes.
class MyClass {}
console.log(MyClass instanceof Object);
3 Comments
Felix Kling
Note however that there can be objects that are not an
instanceof Object: console.log(Object.create(null) instanceof Object).Thomas
also note that primitives also inherit from Object, although they ain't instances of Object:
'use strict'; Object.prototype.foo = function(){ console.log("%o%s an instance of Object, although it has", this, this instanceof Object? "is": "is NOT", foo); } "someText".foo(); Math.PI.foo(); (1 < 2).foo();JLRishe
@Thomas That's not correct. Primitives don't inherit from
Object. Doing a property access on a primitive coerces it into its corresponding wrapper object and wrapper objects inherit from Object. Object.prototype.myVal = 5; var myStr = "a"; console.log(myStr.myVal); myStr.myVal = 7; console.log(myStr.myVal);