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 and can work in many solutions, especially if you do not plan on creating multiple instances of the object. Its limit is that it is unable to create multiple instances of the same objectfor instance, we would not be able to create more than one employee with this method.



Ajax for Web Application Developers
Ajax for Web Application Developers
ISBN: 0672329123
EAN: 2147483647
Year: 2007
Pages: 129
Authors: Kris Hadlock

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net