0
What is constructor in JavaScript ?
If any one knows the answer then give answer in the comment section below ...😊
5 ответов
+ 2
Read the yellow note at the bottom of first slide here 👇
https://www.sololearn.com/learn/JavaScript/2979/?ref=app
+ 1
class SampleClass {
constructor() {
}
}
It implemented in ECMAScript6
+ 1
Good luck to u😊
+ 1
Firstly, do you know what a Class is in JavaScript? If not, it might be a good idea to look up that first, but in general a class is a sort of generalized "plan" for creating an object. There is (as far as I know!) only one Sololearn user with the account name HealyUnit, but there may very well be a user *Class* that has some generic adjectives (we call these "properties") and things it can do (we call these "methods").
So, what's a constructor? In particular, a constructor is a special method (remember: that's something stuff made with our class can "do") that's run when something is created using your Class "plan". So if you create a new account on Sololearn, the first thing it might look for is a constructor method.
The constructor does a number of things, but most importantly it sets up the specific values for those "properties". So while every Sololearn account has a username property, only when mine was created did the SoloLearnAccount class's constructor set "username = HealyUnit".
As an aside, there are also constructor *functions*, which are older and a slightly different topic. Basically, a constructor function was (is) an older way of constructing classes in JavaScript that, in general, looked pretty much like a regular old function:
function SoloLearnAccount(username,password,country){
this.username = username;
this.password = password;
this.country = country;
}
They're less used now, but still worth knowing about.