October 2006
Intermediate to advanced
288 pages
5h 55m
English
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 ...
Read now
Unlock full access