What is constructor in JavaScript ? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

What is constructor in JavaScript ?

If any one knows the answer then give answer in the comment section below ...😊

29th Mar 2021, 4:25 AM
Y U V R A J 💻
Y U V R A J 💻 - avatar
4 Answers
+ 2
Read the yellow note at the bottom of first slide here 👇 https://www.sololearn.com/learn/JavaScript/2979/?ref=app
29th Mar 2021, 5:08 AM
Ipang
+ 1
class SampleClass { constructor() { } } It implemented in ECMAScript6
29th Mar 2021, 4:29 AM
Isabella
Isabella - avatar
+ 1
Good luck to u😊
29th Mar 2021, 4:31 AM
Isabella
Isabella - avatar
+ 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.
30th Mar 2021, 3:26 PM
HealyUnit
HealyUnit - avatar