JavaScript Object Constructors
JavaScript Basics

JavaScript Object Constructors

JavaScript Object Constructors: Overview and Best Practices

JavaScript objects can be constructed in various ways, each suited for different scenarios. Here we explore the different methods to define and manipulate objects, particularly focusing on constructor functions.

JavaScript Object Constructor

Constructor functions are a powerful way to create multiple instances of a similar object type. They allow properties and methods to be encapsulated in a single function, which can be instantiated using the new keyword.

function Person(firstName, lastName, age, eyeColor) 
{  this.firstName = firstName;  
this.lastName = lastName;  this.
age = age;  this.eyeColor = eyeColor;}

Good Practices:
  • Naming: Constructor functions should start with a capital letter to distinguish them from regular functions.
  • The this keyword: Inside a constructor, this refers to the new object being created.
Creating Instances:

To create new objects of the type Person, use the new keyword:

const myFather = new Person("John", "Doe", 50, "blue");

const myMother = new Person("Sally", "Rally", 48, "green");

Customizing Constructors:
  • Adding Properties: Properties can be added directly within the constructor for all future instances.
function Person(firstName, lastName, age, eyeColor) {  
this.firstName = firstName;  
this.lastName = lastName; 
this.age = age;  this.eyeColor = eyeColor; 
this.nationality = "English"; // Default value for all instances}

  • Adding Methods: Similarly, methods should be defined within the constructor to ensure they are available to all instances.

function Person(firstName, lastName, age, eyeColor) this.firstName = firstName; 
this.lastName = lastName;  
this.age = age;  
this.eyeColor = eyeColor;  
this.fullName = function() {    return 
this.firstName + " " + this.lastName;  
};}

Modifying Existing Objects

After an object is created, properties and methods can still be added:

myFather.nationality = "English";myFather.name = function() 
{  return this.firstName + " " + 
this.lastName;};

These modifications will apply only to myFather and not to any other instances of Person.

JavaScript Built-in Constructors

JavaScript also has built-in constructors for creating complex object types:

  • new String()
  • new Number()
  • new Boolean()
  • new Object()
  • new Array()
  • new RegExp()
  • new Function()
  • new Date()

However, it's generally best practice to use primitive values (e.g., literal strings or numbers) for performance and simplicity, unless there's a specific need for object features, such as methods available on prototype objects.

Caution Against Object Constructors

While constructors like new String(), new Number(), and new Boolean() exist, they are not recommended:

  • They complicate the code and lead to performance issues.
  • Primitives are simpler and usually more efficient.
let x1 = "";           
// Use primitive stringlet x2 = 0;     

// Use primitive numberlet x3 = false;        
// Use primitive booleanconst 
x4 = {};          // Use object literal for objectsconst x5 = [];          // Use array literal for arraysconst 
x6 = /()/;        // Use regex literalconst 
x7 = function(){}; // Use function expression

Understanding and using the appropriate object construction and modification methods are crucial for effective JavaScript programming, aligning with best practices and the language's prototypal inheritance model.

Take a look into your desired course