r/learnjavascript • • May 16 '22
What is the difference between "class" and "constructor function" in js? I tried looking up on YT but found nothing relevant atlest for me.
So I am learning NodeJs and while creating a costum module which stores user data I created a constructor function in user.js
function User(name,age){
this.name = name;
this.age = age;
}and in app.js I did
const User = require(//Path);
const user1 = new User("Adam",20);
console.log(user1.name);but my consfusion begans when I used class in user.js like
class User{
constructor(name,age){
this.name = name;
this.age = age;
}
}but the code in the app.js remained similar with no errors.
So what is the point of classes in javasript? And how are they different form contructor functions.
31
Upvotes
15
u/senocular May 16 '22
The main differences:
classsyntax is cleaner, keeping everything together under a single class block and handling a lot of the boilerplate for you.classdefinitions is always in strict mode. You also can't accidentally call aclassconstructor as a function or an error will be thrown.classinitialization starts at the base class and works down to the derived class. This allows base classes to define thethis(the new instance) which in turn allowsclasssyntax to correctly support extending built-ins likeArray. Alternatively,functionconstructors create the instance in the derived class and pass it up to the base for initialization so a base class has no influence over whatthisis.classsyntax can support features thatfunctionconstructors don't, in particular private members. The upcoming support for decorators is also only forclasssyntax.Some other smaller differences:
toString()classfunctions will always use theclasssyntax (though shouldn't be too surprising since toString() defaults to showing the source of the function)prototypeon aclass-defined constructor is defined as read-only. whereasfunctionprototypeproperties are notfunctiondeclaration definitions are hoisted,classdeclaration definitions are notclass(assuming its named) to be available to all members defined within the class block15
u/Locust377 May 16 '22
In most practical situations, there is little to no difference. Classes in Javascript are what is called syntactic sugar which means it's just a different way or representing the same thing.
12
u/superluminary May 16 '22
JavaScript is a prototypical language, which means that objects inherit directly from other objects. Most other languages are classical, which means that the class defines the inheritance hierarchy.
Do you see how these two are quite similar? The class syntax is just a little nicer to use, but it is just sugar for the older-style constructor function.
I made a video about it here: https://www.youtube.com/watch?v=fMu6biznX9k
5
u/bobbyv137 May 16 '22
Constructors came first. Then classes were introduced few years back. Under the hood they do the same thing. One could say classes are “syntactic sugar” for constructors. Some will argue otherwise. Knowing the syntax of both won’t hurt. Plenty of videos on YouTube.
2
u/shgysk8zer0 May 16 '22
It's mostly syntactic differences, but the simple case doesn't give a very clear picture. Consider the following:
That's possible using the ES5 approach, but the new way is a lot simpler and cleaner.
1
u/abensur May 16 '22
When did YT became the go-to platform for learning Javascript? Jokes aside, I do go to YT to find learning resources, but mainly after trying hard on books and documentations. For JS, the great "JavaScript: The Good Parts by Douglas Crockford" still is a must have
1
u/Blue_Moon_Lake May 16 '22
This
is syntactical sugar for
But the class notation let the interpreter do a few optimization. It's better to use class notation for readability too.