Using the new Operator

Using the new operator is probably one of the simplest approaches to creating objects in JavaScript because, of course, it is the native way of doing so. In order to create an object using JavaScript’s native new operator, we simply choose a name for our object and set it equal to a new Object.

var employee = new Object();
employee.id = 001;
employee.firstName = "Kris";
employee.lastName = "Hadlock";

employee.getFullName = function()
{
    return this.firstName + " " + this.lastName;
}

In this sample, we are defining a custom object named employee with id, firstName, and lastName properties, and a method called getFullName, which returns a concatenated version of the firstName and lastName properties. This solution is acceptable ...

Get Ajax for Web Application Developers now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.