Apply for Zend Framework Certification Training

Javascript





Class and Object

Class is nothing but a blueprint for an object of it. It is used to create an object mainly. If we relate it to a real-life example then it is like a plan for a building or house where that plan contains details about doors, windows, floors, etc. Based on the class which is the blueprint an object is made which can be referred to as a house in this example. So one plan is used to make a lot of houses in the same way one class can be used to create a lot of classes. So class is not a real-life entity but the object is one. 

class Company{

   printmyname(){

     console.log("hello")

   }

}  

const myCompanyObject = new Company()

myCompanyObject.printmyname()

Constructor and use of this

The constructor method is a special method in JavaScript with the exact name ‘constructor.’ It is automatically executed when a new object is created from a class. Its primary purpose is to initialize object properties, allowing you to define the initial state of an object during its instantiation.

class Company {

  constructor(name, position) {

    this.name = name;

    this.position = position;

    console.log(

      "MY name is " + this.name + " and My position " + this.position,

    );

  }

  printmyname(grade) {

    this.grade = grade;

    console.log("hello " + this.name + " and my grade is " + this.grade);

  }

  setmyname() {

    console.log("hello set my name" + this.position);

  }

}

const myCompanyObject = new Company("Alok", "Developer");

myCompanyObject.printmyname("5th");

myCompanyObject.setmyname();


 

Inheritance and Super

Inheritance in JavaScript is a way to pass down properties and methods from a parent class to a child class: 

Explanation

Inheritance is a programming technique that allows a new piece of code to reuse and build upon the features of an existing one. In JavaScript, inheritance is implemented using objects, which have an internal link to another object called their prototype.  

How to use inheritance

To create a class inheritance in JavaScript, use the extends keyword. For example, to create a class named Model that inherits methods from the Car class, you can use the syntax class Model extends Car. 

Benefits

Inheritance can help reduce the lines of code in a program, save time and effort, and make the project cleaner and easier to maintain. 

Overriding methods

To override a method in a child class, you can use super.method() in the child method to call the parent method. To override a constructor, you must call the parent constructor as super() in the child constructor before using this

class Company {

  constructor(name, position) {

    this.name = name;

    this.position = position;

    console.log(

      "MY name is " + this.name + " and My position " + this.position,

    );

  }

  printmyname(grade) {

    this.grade = grade;

    console.log("hello " + this.name + " and my grade is " + this.grade);

  }

  setmyname() {

    console.log("hello set my name" + this.position);

  }

}

class employee extends Company {

  constructor(name, position, salary) {

    super(name, position);

    this.salary = salary;

    console.log("my salary is " + this.salary);

  }

}

const myemployeeObject = new employee("alok", "developer", 1000000);

 

< First Getting Started With CodeIgniter URL Routing >



Ask a question



  • Question:
    {{questionlistdata.blog_question_description}}
    • Answer:
      {{answer.blog_answer_description  }}
    Replay to Question


Back to Top