O'Reilly logo

Professional, JavaScript® for Web Developers, Second Edition by Nicholas C. Zakas

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Chapter 18. Advanced Techniques

JavaScript is an incredibly flexible language that can be used in a variety of styles. Typically, JavaScript is used either in a procedural manner or an object-oriented one. The language, however, is capable of much more intricate and interesting patterns because of its dynamic nature. These techniques make use of ECMAScript language features, BOM extensions, and DOM functionality to achieve powerful results.

Advanced Functions

Functions are one of the most interesting parts of JavaScript. They can be quite simple and procedural in nature, or they can be quite complex and dynamic. Additional functionality can be achieved through the use of closures. Additionally, function pointers are very easy to work with, since all functions are objects. All of this makes JavaScript functions both interesting and powerful. The following sections outline some of the advanced ways that functions can be used in JavaScript.

Scope-Safe Constructors

Chapter 6 covered the definition and usage of constructors for defining custom objects. You'll recall that a constructor is simply a function that is called using the new operator. When used in this way, the this object used inside the constructor points to the newly created object instance, as in this example:

function Person(name, age, job){
    this.name = name;
    this.age = age;
    this.job = job;
}

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

In this example, the Person constructor assigns three properties using the ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required