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:
function Person(name, age, gender) {
this.name = name;
this.age = age;
this.gender = gender;
}
var person1 = new Person("John", 25, "male");
var person2 = new Person("Jane", 30, "female");
console.log(person1.name);
console.log(person2.age);
Person.prototype.sayHello = function () {
console.log(
"Hello, my name is " + this.name + " and I am " + this.age + " years old."
);
};
person1.sayHello();
person2.sayHello();
In the above example:
Person
is an object constructor function that takes three parameters (name
, age
, and gender
) and assigns them as properties on the newly created objects using the this
keyword.- The
new
keyword is used to create new objects based on the Person
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:
var person = {
name: "John",
age: 25,
sayHello: function () {
console.log(
"Hello, my name is " + this.name + " and I am " + this.age + " years old."
);
},
};
person.sayHello();
In the above example:
person
is an object with properties name
and age
, as well as a method sayHello
.- Inside the
sayHello
method, this refers to the person
object, which allows you to access its properties name
and age
using dot notation (this.name
and this.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:
function Person(name, age, gender) {
this.name = name;
this.age = age;
this.gender = gender;
}
Person.prototype.country = "Unknown";
var person1 = new Person("John", 25, "male");
var person2 = new Person("Jane", 30, "female");
console.log(person1.country);
console.log(person2.country);
person1.country = "USA";
console.log(person1.country);
console.log(person2.country);
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 using Person.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:
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.
The Array
constructor is used to create arrays, which are collections of values in JavaScript.
The String
constructor is used to create string objects, which represent a sequence of characters in JavaScript.
var str = new String("Hello");
The Number
constructor is used to create number objects, which represent numeric values in JavaScript.
var num = new Number(42);
The Boolean
constructor is used to create boolean objects, which represent true or false values in JavaScript.
var bool = new Boolean(true);
The Date
constructor is used to create date objects, which represent dates and times in JavaScript.
RegExp
The RegExp
constructor is used to create regular expression objects, which are used for pattern matching in strings.
var regex = new RegExp("^\\d+$");