I assume that an ES6 class is an object as "everything" is objects in JavaScript. Is that a correct assumption?
-
1classes are objects, but not "everything" is an object. Primitives (strings, numbers etc) can be autoboxed to objects, but they are not objects per se.georg– georg2018-03-16 12:45:23 +00:00Commented Mar 16, 2018 at 12:45
-
"Classes" are just functions and functions are objects.Felix Kling– Felix Kling2018-03-17 15:38:13 +00:00Commented Mar 17, 2018 at 15:38
6 Answers
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);
6 Comments
java.lang.Class), moreover, even a metaclass (a class of classes) is an object of its own.typeof null === 'object'.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
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
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
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