SEARCH
0-9 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
Prev | Current Page 88 | Next

Jonathan Snook, Aaron Gustafson, Stuart Langridge, and Dan Webb

"Accelerated DOM Scripting with Ajax, APIs, and Libraries"

The function expects the
parameter to be a function that it can execute.
3. Run the function passing the method of the el object in as a parameter. The myfunc()
function then executes the method.
Here??™s where it??™s a little confusing. Even though you are executing el.methodname(), you
passed in a reference only to the function, not to the whole object. Therefore, when you execute
the function and it tries to alert this, it returns the window object because myfunc()
belongs to the window object.
This can be both an advantage and a disadvantage, depending on what your needs are.
Luckily, JavaScript offers a call() method that enables you to execute a method in the context
of another object:
function myfunc(func)
{
func.call(el);
}
In this example, the func function gets executed??”but in the context of the el object.
Therefore, when using this, it refers to el.
Many of the JavaScript libraries out there create their own methods for binding objects
(this topic is discussed in more detail in Chapter 3).
Coming back to the example, you need to update the addListener() function to use
call() to pass through the correct context. An anonymous function is used to encapsulate the
reference:
function addListener(element, event, listener) {
if (element.addEventListener){
element.addEventListener(event, listener, false);
} else if (element.attachEvent){
element.attachEvent('on'+event, function(){listener.


Pages:
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
hotel jelenia góra Russian bride Free English grammar and study guid powiekszenia wielkoformatowe counter strike 1.6