Objects and Classes

Constructor Functions and the ‘new’ Keyword

In JavaScript, a constructor function is a special type of function that is used to create new objects. When a constructor function is called with the new keyword, a new object is created and the constructor function is executed. The constructor function can then set the properties and methods of the new object.

For example, the following code defines a constructor function called Person:

function Person(name, age) {
  this.name = name;
  this.age = age;
}

The Person constructor function takes two parameters: name and age. When the Person constructor function is called with the new keyword, a new object is created and the Person constructor function is executed. The name and age parameters are passed to the Person constructor function, and the values of the parameters are assigned to the name and age properties of the new object.

The following code creates a new object from the Person constructor function:

var person = new Person("John Doe", 30);

The person variable now refers to a new object that has a name property with the value “John Doe” and an age property with the value 30.

Working with Properties and Methods

Objects in JavaScript can have properties and methods. Properties are data that is associated with an object, and methods are functions that are associated with an object.

Properties can be accessed using the . operator. For example, the following code gets the name property of the person object:

var name = person.name;

The name variable will now have the value “John Doe”.

Methods can be called using the . operator. For example, the following code calls the greet() method of the person object:

person.greet();

The greet() method will print the following message to the console:

Hello, my name is John Doe!

Prototype-based Inheritance in JavaScript

JavaScript uses prototype-based inheritance. This means that when a new object is created from a constructor function, the new object inherits the properties and methods of the constructor function’s prototype object.

The prototype object of a constructor function is an object that is created when the constructor function is defined. The prototype object can be accessed using the prototype property of the constructor function.

For example, the following code defines a constructor function called Animal:

function Animal() {
}

Animal.prototype.name = "Animal";
Animal.prototype.greet = function() {
  console.log("I am an animal!");
};

The Animal constructor function has a name property and a greet() method. The name property is set to the value “Animal”, and the greet() method prints the message “I am an animal!” to the console.

The Animal constructor function’s prototype object is an object that has the name property and the greet() method. When a new object is created from the Animal constructor function, the new object inherits the name property and the greet() method from the Animal constructor function’s prototype object.

Conclusion

Constructor functions, properties, and methods are the basic building blocks of object-oriented programming in JavaScript. By understanding these concepts, you can create powerful and reusable objects.