5

I assume that an ES6 class is an object as "everything" is objects in JavaScript. Is that a correct assumption?

2
  • 1
    classes are objects, but not "everything" is an object. Primitives (strings, numbers etc) can be autoboxed to objects, but they are not objects per se. Commented Mar 16, 2018 at 12:45
  • "Classes" are just functions and functions are objects. Commented Mar 17, 2018 at 15:38

6 Answers 6

10

From the point of view of Object Oriented Programming class is not an object. It is an abstraction. And every object of that is a concrete instance of that abstraction.

From the point of view of JavaScript, class is an object, because class is a ES6 feature and under it simple function is used. It is not just an abstraction in Javascript, but also an object by itself. And that function is an object. It has it's own properties and functions.

So speaking in Javascript not everything is an object. There are also primitive types - number, string, boolean, undefined, symbol from ES6. When you will use some methods with this primitive types except undefined, they will be converted into the objects.

You can see the below example.

const str = 'Text';
const strObj = new String('Text');

console.log(str);
console.log(strObj.toString());

console.log(typeof str);
console.log(typeof strObj);

There is also one extra primitive type null, but checking it's type returns an object. This is a bug.

console.log(typeof null);

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

6 Comments

I don't think this is correct. In most OO languages, classes are objects (cf. java.lang.Class), moreover, even a metaclass (a class of classes) is an object of its own.
Suren is not saying that classes in other languages are not objects. He is saying that in the concept of OOP, a class is not an object, but a type of object
@georg I am speaking about OOP globally, not for some languages
This is the most incomprehensible technically correct answer I can imagine!
Since the answer explains the primitives, you could also mention symbol primitive for completion sake. And the funniest JS quirk ever, typeof null === 'object'.
|
10

ES6 class is a function, and any function is an object:

(class {}).constructor === Function
(class {}) instanceof Function === true
(class {}) instanceof Object === true

Although not every object is Object instance:

Object.create(null) instanceof Object === false

The statement that everything is an object is not correct. That's an oversimplified way to say that some primitive values in JavaScript can be coerced to objects:

(1).constructor === Number
1 instanceof Number === false 

But primitives aren't objects until they are converted:

Object(1) instanceof Number === true
Object(1) instanceof Object === true

And null and undefined can't be coerced at all:

null instanceof Object === false
Object(null) instanceof Object === true // conversion
(null).constructor // coercion error
typeof null === 'object' // JS bug

1 Comment

This is a clean and helpful answer.
2

Yes, it is.

If you do:

class Hello { };

then Hello is in itself an object an can be used like any other:

console.log(Hello.toString()); // outputs 'class Hello { }'

As you say, eveything in JavaScript is an object

In fact:

console.log(Hello instanceof Object); // prints 'true'

1 Comment

We're not speaking about OOP or its concepts, but about JavaScript in particular.
2

The real question is whether Javascript classes are classes!

In reality they are not. Javascript's object model is prototype-based, rather than class-based, which makes it hard to understand. The class syntax is a useful tool, but it is really only syntactic sugar to make the model easier to understand.

In reality, a JS class is actually a function, with a prototype associated with it.

class Hippo{};
Hippo.__proto__ === Function.prototype; // true

All JS functions are also objects.

Hippo.__proto__.__proto__ === Object.prototype; // true

The prettier way to say this is

Hippo instanceof Object; // true

So a JS class is an object and a function -- but it isn't really a class!

4 Comments

JS class is an object and a function -- but it isn't really a class. I think Javascript is a bit incomprehensible )
@SurenSrapyan Undoubtedly.
Javascript breaks OOP.
"In reality they are not." So, what makes a class a class?
0

Class is a function in Javascript, Only function has prototype property, not the object.

Check code below

class MyClass {}
MyClass.prototype.value = 'My Class Value'; // Works same as function
const myclass = new MyClass();
console.log(myclass.value);

function MyFunc() {}
MyFunc.prototype.value = 'My Function Value';
const myfunc = new MyFunc();
console.log(myfunc.value);

var MyObject = {};
MyObject.prototype.value = 10; // Throws an error

Comments

0

This is a good question. From the OOP 001, Object is an instance of the class. So at no point can class be an object again

Comments

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.