How the New Keyword Works in JavaScript

JavaScript is a powerful and versatile programming language that is widely used in web development. One of the most important concepts in JavaScript is the new keyword, which is used to create new instances of objects. In this article, we will discuss how the new keyword works in JavaScript, including how it uses prototype inheritance instead of classical inheritance, and provide code samples illustrating its use.

What is the new keyword?

The new keyword is used to create new instances of objects in JavaScript. When used with a constructor function, it creates a new object and sets the prototype of the new object to the prototype property of the constructor function. The new keyword also returns the new object, so it can be assigned to a variable for later use.

How does JavaScript use prototype inheritance instead of classical inheritance?

JavaScript uses prototype inheritance instead of classical inheritance, which is used in languages like Java and C++. In classical inheritance, objects are created from classes, which are like blueprints for objects. In JavaScript, however, objects are created from constructor functions, which are similar to classes but with some important differences.

One of the key differences is that in JavaScript, objects inherit properties and methods from their prototypes, rather than from classes. A prototype is an object that is associated with a constructor function, and any properties or methods added to the prototype are inherited by objects created from that constructor function.

This approach to inheritance is known as prototype inheritance, and it allows for a more flexible and dynamic way of creating and modifying objects.

Code samples illustrating the use of the new keyword

Here are some code samples that illustrate the use of the new keyword in JavaScript:

// Define a constructor function
function Person(name, age) {
  this.name = name;
  this.age = age;
}

// Create a new instance of the Person object using the new keyword
const john = new Person("John", 30);

// Access the properties of the new object
console.log(john.name); // Output: 'John'
console.log(john.age); // Output: 30

In this example, we define a constructor function called Person that takes two parameters: name and age. We then create a new instance of the Person object using the new keyword and assign it to the variable john. We can then access the properties of the new object using dot notation.

// Define a constructor function
function Animal(name) {
  this.name = name;
}

// Add a method to the prototype of the Animal object
Animal.prototype.sayName = function () {
  console.log("My name is " + this.name);
};

// Create a new instance of the Animal object using the new keyword
const cat = new Animal("Fluffy");

// Call the sayName method on the new object
cat.sayName(); // Output: 'My name is Fluffy'

In this example, we define a constructor function called Animal that takes a parameter called name. We then add a method called sayName to the prototype of the Animal object. We create a new instance of the Animal object using the new keyword and assign it to the variable cat. We can then call the sayName method on the new object to output its name.

Conclusion

The new keyword is an important concept in JavaScript that is used to create new instances of objects. JavaScript uses prototype inheritance instead of classical inheritance, which allows for a more flexible and dynamic way of creating and modifying objects. By understanding how the new keyword works and how prototype inheritance works in JavaScript, you can become a more proficient and effective JavaScript developer.

Need Help with Your Website

If you need help with your website contact me here.

© 2023, Elizabeth Rogers All Rights Reserved