Add a method to a JavaScript object constructor?

JavascriptObject Oriented ProgrammingFront End Technology

Adding a method to an object constructor is unlike adding a method to a normal object. We cannot add a method as it is the case with a normal object. To make a method in an object constructor it has to be added inside the object constructor.


In the following example, the method is added inside the constructor, therefore, we have got a legitimate value.

Live Demo

   function Business(name, property, age, designation) {
      this.Name = name;
      this.prop = property;
      this.age = age;
      this.designation = designation; = function() {
         return this.Name
   var person1 = new Business("Trump", "$28.05billion", "73", "President");


Updated on 03-Jul-2020 08:27:52