Practice
Resources
Contests
Online IDE
New
Free Mock
Scaler
Practice
Improve your coding skills with our resources
Contests
Compete in popular contests with top coders
Scaler
Explore Offerings by SCALER

Begin your success journey!

Sign Up using
Full name *
Email *
Password *

By creating an account, I acknowledge that I have read and agree to InterviewBit’s Terms and Privacy Policy .

Welcome back!

Log In using
Email *
Password *

Classes And Their Instances

What are classes?

Classes are a template/blueprint for creating objects. They encapsulate data with code to work on that data. Classes in JS are built on prototypes.

In Modern Javascript, classes act as special functions.

 

Class Declaration

To declare a class we can use a special keyword class followed by class name and then pair of curly braces which wraps the body of the class.

class Student{

//Body of class

}

One can also declare a class as:

const Student = class {
// Body of class
};

Instance of a class

An instance is an object containing data and behavior described by the class.

We can create an instance of a class (initiate the class), by using new keyword.

var student1 = new Student();  //student1 is now an instance of class Student

We can initialise the data that an instance may contain by defining a constructor function in the class body which is called whenever a class instance is created.

class Student{
constructor(name, age) {
this.name = name,
this.age = age
}
}

const student1 = new Student("max", 20)
console.log(student1.name) //prints max
console.log(student1.age) //prints 20

 

static 

The static keyword defines a static method or property for a class. Neither static methods nor static properties can be called on instances of the class. Instead, they’re called on the class itself. Static methods are often utility functions.

We dont need to create any instance to call or access the static members.

class A{
    static propA = "propA";
    static methodA() {
        console.log("methodA");
    }
}

console.log(A.propA); //prints "propA"
A.methodA();  //prints "methodA"
const a1 = new A(); //creating an instance of class A
console.log(a1.propA); //prints undefined (propA is not a member of instance a1)
a1.methodA(); //gives an error

 

Try the following example in the editor below.

For the given class Employee, define a constructor function so that we can initialise the name, position and skill properties with default input strings while creating instances of the class.

Sample Input

 Max
 SDE1
 C++

 

Start solving Classes And Their Instances on Interview Code Editor
Hints
  • Hints are not available for this problem

Discussion


Loading...
Click here to start solving coding interview questions
Free Mock Assessment
Fill up the details for personalised experience.
All fields are mandatory
Current Employer *
Enter company name *
Graduation Year *
Select an option *
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
Phone Number *
OTP will be sent to this number for verification
+1 *
+1
Change Number
Phone Number *
OTP will be sent to this number for verification
+1 *
+1
Change Number
Graduation Year *
Graduation Year *
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
*Enter the expected year of graduation if you're student
Current Employer *
Company Name *
Please verify your phone number
Edit
Resend OTP
By clicking on Start Test, I agree to be contacted by Scaler in the future.
Already have an account? Log in
Free Mock Assessment
Instructions from Interviewbit
Start Test