Literal Notation


Literal notation is a more complex way of defining objects with JavaScript and is supported in JavaScript 1.2 and above. This approach is a sort of shorthand way of creating objects, which makes them easy to build but a bit hard to read because of the unique syntax that they require. Here is a sample of creating the same object (with the literal notation approach) we created with the new operator.

 employee = {        id : 001;        firstName : "Kris";        lastName : "Hadlock";        getFullName : function()        {            return this.firstName + " " + this.lastName;        } } alert(employee.getFullName); // Results: Kris Hadlock


As you can see, this approach is easy to create, but could be rather hard to manage if we were to add a lot of methods and properties. Don't get me wrong, thoughthis is a viable solution, and I wouldn't necessarily recommend against it. It's just that I personally prefer to use a more familiar syntax.



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

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