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.

Chapter 18: Advanced Techniques<br />

Lazy loading functions have two primary advantages. First, the evaluation of the appropriate code to<br />

execute only happens if you actually call the function. Some JavaScript libraries start out by performing<br />

multiple code branches to set everything up ahead of time based on browser capabilities or quirks. Lazy<br />

loading pushes these calculations off until the last possible moment, ensuring that the proper<br />

functionality is available without affecting initial script execution time. Second, although the first call to<br />

a function is slightly slower as a result of the addition of a second function call, all subsequent calls to<br />

the function are faster, because they avoid evaluating multiple if conditions.<br />

Function Binding<br />

An advanced technique that has become increasingly popular is function binding. Function binding<br />

involves creating a function that calls another function in a particular context and with specific<br />

arguments. This technique is often used in conjunction with callbacks and event handlers to preserve<br />

code execution context while passing functions around as variables. Consider the following example:<br />

var handler = {<br />

message: “Event handled”,<br />

};<br />

handleClick: function(event){<br />

alert(this.message);<br />

}<br />

var btn = document.getElementById(“my-btn”);<br />

EventUtil.addHandler(btn, “click”, handler.handleClick);<br />

In this example, an object called handler is created. The handler.handleClick() method is assigned as<br />

an event handler to a DOM button. When the button is clicked, the function is called, and an alert is<br />

displayed. Even though it may seem as if the alert should display “ Event handled ”, it actually displays<br />

“ undefined ” . The problem is that the context of handler.handleClick() is not being saved, so the<br />

this object ends up pointing to the DOM button instead of handler . You can fix this problem using a<br />

closure as shown in the following example:<br />

var handler = {<br />

message: “Event handled”,<br />

};<br />

handleClick: function(event){<br />

alert(this.message);<br />

}<br />

var btn = document.getElementById(“my-btn”);<br />

EventUtil.addHandler(btn, “click”, function(event){<br />

handler.handleClick(event);<br />

});<br />

This solution uses a closure to call handler.handleClick() directly inside the onclick event<br />

handler. Of course, this is a very specific solution to this specific piece of code. Creating multiple<br />

closures can lead to code that is difficult to understand and debug. Therefore, many JavaScript libraries<br />

have implemented a function that can bind a function to a specific context. Typically, this function is<br />

called bind() .<br />

594

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

Saved successfully!

Ooh no, something went wrong!