Loading Please Wait...
The Object() constructor turns the input into an object. Its behavior depends on the input's type.
function User(name, email) {
this.name = name;
this.email = email;
}
const employee = new User("John Doe", "john@gmail.com");
alert(employee.name); // "John Doe"
alert(employee.email); // "john@gmail.com"
It is considered good practice to name constructor functions with an upper-case first letter.
In a constructor function this does not have a value. It is a substitute for the new object. The value of this will become the new object when a new object is created.
Objects created by object literal or new Object() are limited and create only single object.
Sometimes we need a "blueprint" for creating many objects of the same "type".
The way to create an "object type", is to use an object constructor function. This way we can create multiple objects of the same type.
function Employee(name, email) {
this.name = name;
this.email = email;
}
const manager = new Employee("John Doe", "john@gmail.com");
const receptionist = new Employee("Neelam Singh", "neelam@gmail.com");
Adding a new property to an existing object is easy. The following example add the age property to manager.
Note that the property will be added to manager only, not to receptionist. (Not to any other Employee objects).
manager.age = 18;
Adding a new method to an existing object is easy. The following example add the greeting method to manager.
Note that the method will be added to manager only, not to receptionist. (Not to any other Employee objects).
manager.greeting = function () {
return "Hello " + this.name;
};
You cannot add a new property to an object constructor the same way you add a new property to an existing object.
To add a new property to a constructor, you must add it to the constructor function. This way object properties can have default values.
Employee.age = 18;
// or
function Employee(name, email) {
this.name = name;
this.email = email;
this.age = 18;
}
Your constructor function can also define methods. You cannot add a new method to an object constructor the same way you add a new method to an existing object.
Adding methods to an object constructor must be done inside the constructor function.
function Employee(name, email) {
this.name = name;
this.email = email;
this.greeting = function () {
return "Hello " + this.name;
};
}
JavaScript has built-in constructors for native objects.
new String(); // A new String object
new Number(); // A new Number object
new Boolean(); // A new Boolean object
new Object(); // A new Object object
new Array(); // A new Array object
new RegExp(); // A new RegExp object
new Function(); // A new Function object
new Date(); // A new Date object
As you can see above, JavaScript has object versions of the primitive data types String, Number, and Boolean. But there is no reason to create complex objects. Primitive values are much faster.
let x1 = ""; // new primitive string
let x2 = 0; // new primitive number
let x3 = false; // new primitive boolean
const x4 = {}; // new Object object
const x5 = []; // new Array object
const x6 = /()/ // new RegExp object
const x7 = function(){}; // new function
How you feel about this blog:
Share this blog on:
If you find any error in the turtorials, or want to share your suggestion/feedback, feel free to send us email at: info@lynxsia.com
Contact UsWe are concern with various development process like website design & development, E-commerce development, Software development, Application development, SMS & Bulk SMS Provider, PWA Development, and many more..
Copyright ©
, Lynxsia IT Solutions, All rights reserved