Our Recommendation for You Search your Query, You can find easily. for example search by book name or course name or any other which is related to your education

label name

PHP

Object Constructors

JavaScript Object ConstructorsJAVASCRIPT


Using an Object Constructor

The examples from the previous chapters are limited in many situations. They only create single objects.
Sometimes we like to have an "object type" that can be used to create many objects of one type.
The standard way to create an "object type" is to use an object constructor function:

Example

function Person(first, last, age, eye) {
    this.firstName = first;
    this.lastName = last;
    this.age = age;
    this.eyeColor = eye;
}
varmyFather = new Person("John", "Doe", 50, "blue");
var myMother = new Person("Sally", "Rally", 48, "green");
The above function (Person) is an object constructor.
It is considered good practice to name constructor function with an upper-case first letter.
Once you have an object constructor, you can create new objects of the same type:
var myFather = new Person("John", "Doe", 50, "blue");
var myMother = new Person("Sally", "Rally", 48, "green");


The this Keyword

In JavaScript, the thing called this, is the object that "owns" the JavaScript code.
The value of this, when used in a function, is the object that "owns" the function.
The value of this, when used in an object, is the object itself.
The this keyword in an object constructor does not have a value. It is only a substitute for the new object.
The value of this will become the new object when the constructor is used to create an object.
Note that this is not a variable. It is a keyword. You cannot change the value of this.

Built-in JavaScript Constructors

JavaScript has built-in constructors for native objects:

Example

var x1 = new Object();    // A new Object objectvar x2 = new String();    // A new String objectvar x3 = new Number();    // A new Number objectvar x4 = new Boolean();   // A new Boolean objectvar x5 = new Array();     // A new Array objectvar x6 = new RegExp();    // A new RegExp objectvar x7 = new Function();  // A new Function objectvar x8 = new Date();      // A new Date object
The Math() object is not in the list. Math is a global object. The new keyword cannot be used on Math.

Did You Know?

As you can see, JavaScript has object versions of the primitive data types String, Number, and Boolean.
There is no reason to create complex objects. Primitive values execute much faster.
And there is no reason to use new Array(). Use array literals instead: []
And there is no reason to use new RegExp(). Use pattern literals instead: /()/
And there is no reason to use new Function(). Use function expressions instead: function () {}.
And there is no reason to use new Object(). Use object literals instead: {}

Example

var x1 = {};            // new objectvar x2 = "";            // new primitive stringvar x3 = 0;             // new primitive numbervar x4 = false;         // new primitive booleanvar x5 = [];            // new array objectvar x6 = /()/           // new regexp objectvar x7 = function(){};  // new function object

String Objects

Normally, strings are created as primitives: var firstName = "John"
But strings can also be created as objects using the new keyword: var firstName = new String("John")
Learn why strings should not be created as object in the chapter JS Strings.

Number Objects

Normally, numbers are created as primitives: var x = 123
But numbers can also be created as objects using the new keyword: var x = new Number(123)
Learn why numbers should not be created as object in the chapter JS Numbers.

Boolean Objects

Normally, booleans are created as primitives: var x = false
But booleans can also be created as objects using the new keyword: var x = new Boolean(false)
Learn why booleans should not be created as object in the chapter JS Booleans.