topical media & game development
#javascript-code-17-context.js / js
var obj = {
yes: function(){
// this == obj
this.val = true;
},
no: function(){
this.val = false;
}
};
// We see that there is no val property in the 'obj' object
alert( obj.val == null );
// We run the yes function and it changes the val property
// associated with the 'obj' object
obj.yes();
alert( obj.val == true );
// However, we now point window.no to the obj.no method and run it
window.no = obj.no;
window.no();
// This results in the obj object staying the same (as the context was
// switched to the window object)
alert( obj.val == true );
// and window val property getting updated.
alert( window.val == false );
(C) Æliens
20/2/2008
You may not copy or print any of this material without explicit permission of the author or the publisher.
In case of other copyright issues, contact the author.