0

Functions in Javascript are objects. And the class keyword in ES6 is a syntactical sugar and actually is a function, so classes are objects?

2
  • 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? Commented 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'. Commented Apr 22, 2017 at 21:32

1 Answer 1

4

Yes.

class MyClass {}

console.log(MyClass instanceof Object);

Sign up to request clarification or add additional context in comments.

3 Comments

Note however that there can be objects that are not an instanceof Object: console.log(Object.create(null) instanceof Object).
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();
@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);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.