Learning from jQuery by Callum Macrae
Author:Callum Macrae
Language: eng
Format: epub, pdf
Tags: COMPUTERS / Programming Languages / JavaScript
ISBN: 9781449345259
Publisher: O’Reilly Media
Published: 2012-08-22T16:00:00+00:00
Literals Notation
JavaScript has literal notations available for stuff like creating new objects and arrays, which allow you to shorten your code and make it clearer. It is better to use literals than the constructor functions, such as new Object() and new Array(), and I’ll explain why using a few examples.
Object Literals
The object literal simply uses curly braces. Here are two ways of defining exactly the same object:
// Using the literal syntax var obj = {}; // Using the constructor function (antipattern) var obj = new Object();
The most obvious reason to use the literal syntax instead of the constructor function is that it is shorter to type, but there are a couple more reasons. The literal syntax never has scoping issues—if, for example, you accidentally call a variable Object, you would overwrite the object constructor, and you would not be able to create any more objects using the constructor function. This can never happen to the literal syntax. In a similar vein, the parser doesn’t have to do any scope resolution with the literal syntax. If you use the literal syntax, JavaScript knows exactly what you mean and gives you an object. If you use the constructor function, however, then JavaScript has to check the current scope, then cycle up through the parent scopes until it finds the Object constructor. The first uses fewer resources and is slightly quicker. Finally, you can declare properties using the literal notation:
// Using the literal syntax var obj = { foo: 'bar', hello: 'world' }; // Using the constructor function (antipattern) var obj = new Object(); obj.foo = 'bar'; obj.hello = 'world';
Download
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
Sass and Compass in Action by Wynn Netherland Nathan Weizenbaum Chris Eppstein Brandon Mathis(7799)
Grails in Action by Glen Smith Peter Ledbrook(7712)
Secrets of the JavaScript Ninja by John Resig Bear Bibeault(6432)
Kotlin in Action by Dmitry Jemerov(5080)
WordPress Plugin Development Cookbook by Yannick Lefebvre(3901)
Mastering Azure Security by Mustafa Toroman and Tom Janetscheck(3342)
Learning React: Functional Web Development with React and Redux by Banks Alex & Porcello Eve(3096)
Mastering Bitcoin: Programming the Open Blockchain by Andreas M. Antonopoulos(2882)
The Art Of Deception by Kevin Mitnick(2619)
Drugs Unlimited by Mike Power(2476)
The Innovators: How a Group of Hackers, Geniuses, and Geeks Created the Digital Revolution by Walter Isaacson(2411)
A Blueprint for Production-Ready Web Applications: Leverage industry best practices to create complete web apps with Python, TypeScript, and AWS by Dr. Philip Jones(2344)
Kali Linux - An Ethical Hacker's Cookbook: End-to-end penetration testing solutions by Sharma Himanshu(2317)
Writing for the Web: Creating Compelling Web Content Using Words, Pictures and Sound (Eva Spring's Library) by Lynda Felder(2271)
SEO 2018: Learn search engine optimization with smart internet marketing strategies by Adam Clarke(2198)
JavaScript by Example by S Dani Akash(2146)
DarkMarket by Misha Glenny(2090)
Wireless Hacking 101 by Karina Astudillo(2087)
Hands-On Cybersecurity with Blockchain by Rajneesh Gupta(2048)
