Javascript Object Constructors
The object constructors are functions that are used to create objects with similar properties and methods.
Object constructors are commonly used in object-oriented programming to create objects based on a blueprint or template.
They allow you to create multiple objects with similar properties and methods without having to define them individually.
Here's an example of how you can define and use an object constructor:
// Define an object constructor function
function Person(name, age, gender) {
this.name = name;
this.age = age;
this.gender = gender;
}
// Create objects using the constructor function
var person1 = new Person("John", 25, "male");
var person2 = new Person("Jane", 30, "female");
// Access properties of objects
console.log(person1.name); // Output: "John"
console.log(person2.age); // Output: 30
// Add methods to the constructor's prototype
Person.prototype.sayHello = function () {
console.log(
"Hello, my name is " + this.name + " and I am " + this.age + " years old."
);
};
// Call methods on objects
person1.sayHello(); // Output: "Hello, my name is John and I am 25 years old."
person2.sayHello(); // Output: "Hello, my name is Jane and I am 30 years old."
In the above example:
Person
is an object constructor function that takes three parameters (name
,age
, andgender
) and assigns them as properties on the newly created objects using thethis
keyword.- The
new
keyword is used to create new objects based on thePerson
constructor. - You can then access and modify the properties of these objects like any other JavaScript object.
What is this
?
this
is a reference to the current object that a method is called on. When a function is invoked as a method of an object, this
refers to that object, allowing you to access and modify its properties and methods.
In other words, this
in the context of an object refers to the instance of that object on which the method is called.
Here's an example to illustrate the concept of this
in the object sense:
// Define an object
var person = {
name: "John",
age: 25,
sayHello: function () {
console.log(
"Hello, my name is " + this.name + " and I am " + this.age + " years old."
);
},
};
// Call the sayHello method on the person object
person.sayHello(); // Output: "Hello, my name is John and I am 25 years old."
In the above example:
person
is an object with propertiesname
andage
, as well as a methodsayHello
.- Inside the
sayHello
method, this refers to theperson
object, which allows you to access its propertiesname
andage
using dot notation (this.name
andthis.age
).
Adding a Property to a Constructor
You can add properties to a constructor function by extending its prototype
object. The properties added to the prototype
object are shared among all objects created by that constructor, and they can be accessed and modified by those objects.
Here's an example of how you can add a property to a constructor function:
// Define a constructor function
function Person(name, age, gender) {
this.name = name;
this.age = age;
this.gender = gender;
}
// Add a property to the prototype of the Person constructor
Person.prototype.country = "Unknown";
// Create objects using the Person constructor
var person1 = new Person("John", 25, "male");
var person2 = new Person("Jane", 30, "female");
// Access the property on the objects
console.log(person1.country); // Output: "Unknown"
console.log(person2.country); // Output: "Unknown"
// Modify the property on one object
person1.country = "USA";
console.log(person1.country); // Output: "USA"
console.log(person2.country); // Output: "Unknown"
In the above example:
- The
Person
constructor is defined with name, age, and gender as properties. - Then, a new property country is added to the prototype of the
Person
constructor usingPerson.prototype.country = "Unknown"
. - This property is now shared among all objects created by the
Person
constructor. - You can access and modify the country property on any object created by the
Person
constructor, and the changes will be reflected on that specific object.
Adding properties to the prototype
of a constructor allows you to define common properties or methods that can be shared among all objects created by that constructor, saving memory and improving performance.
Built-in JavaScript Constructors
JavaScript has several built-in constructors that are available as part of the JavaScript language itself. These constructors are used to create objects of various types and are part of the JavaScript standard library. Some of the commonly used built-in constructors in JavaScript include:
Object
The Object
constructor is used to create objects in JavaScript. It is the base constructor for all objects in JavaScript, and other objects inherit from it.
var obj = new Object(); // Creates a new empty object
Array
The Array
constructor is used to create arrays, which are collections of values in JavaScript.
var arr = new Array(); // Creates a new empty array
String
The String
constructor is used to create string objects, which represent a sequence of characters in JavaScript.
var str = new String("Hello"); // Creates a string object with value "Hello"
Number
The Number
constructor is used to create number objects, which represent numeric values in JavaScript.
var num = new Number(42); // Creates a number object with value 42
Boolean
The Boolean
constructor is used to create boolean objects, which represent true or false values in JavaScript.
var bool = new Boolean(true); // Creates a boolean object with value true
Date
The Date
constructor is used to create date objects, which represent dates and times in JavaScript.
var date = new Date(); // Creates a date object representing the current date and time
RegExp
The RegExp
constructor is used to create regular expression objects, which are used for pattern matching in strings.
var regex = new RegExp("^\\d+$"); // Creates a regular expression object for matching digits