04.11.2015 Views

javascript

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Advanced Techniques<br />

JavaScript is an incredibly flexible language that can be used in a variety of styles.<br />

Typically, JavaScript is used either in a procedural manner or an object - oriented one. The language,<br />

however, is capable of much more intricate and interesting patterns because of its dynamic nature.<br />

These techniques make use of ECMAScript language features, BOM extensions, and DOM<br />

functionality to achieve powerful results.<br />

Advanced Functions<br />

Functions are one of the most interesting parts of JavaScript. They can be quite simple and<br />

procedural in nature, or they can be quite complex and dynamic. Additional functionality can be<br />

achieved through the use of closures. Additionally, function pointers are very easy to work with,<br />

since all functions are objects. All of this makes JavaScript functions both interesting and powerful.<br />

The following sections outline some of the advanced ways that functions can be used in JavaScript.<br />

Scope - Safe Constructors<br />

Chapter 6 covered the definition and usage of constructors for defining custom objects. You ’ ll<br />

recall that a constructor is simply a function that is called using the new operator. When used in<br />

this way, the this object used inside the constructor points to the newly created object instance, as<br />

in this example:<br />

function Person(name, age, job){<br />

this.name = name;<br />

this.age = age;<br />

this.job = job;<br />

}<br />

var person = new Person(“Nicholas”, 29, “Software Engineer”);

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!