This is a classic Javascript stumbling block, relating to how the "this" key is scoped within a closure. To illustrate.
This is a classic Javascript stumbling block, relating to how the "this" key is scoped within a closure. To illustrate: redPrinter = function() { this. X = 'red'; return function() { return this.
X; } } main = function() { this. X = 'blue'; var myRedPrinter = new redPrinter(); alert("Red printer returned: " + myRedPrinter()); } main(); This code will print out: Red printer returned: blue because the scope of 'this' in the line: return this. X is actually tied to the main() object at the time of invocation.
There are generally 2 ways to address this: 1) Avoid using the 'this' keyword within a function closure. To fix your code this way, I would simply collect all of the event bindings in one place instead of cascading them, thus removing the 'this' references from both 'displaydown()' and 'displaymove()': test. Prototype = { init: function () { addEvent(document, 'mousedown', this.
Displaydown); addEvent(document, 'mousemove', this. Displaymove); }, displaydown : function(e){ document. GetElementById('mydiv').
InnerHTML = "down"; }, displaymove : function(e){ document. GetElementById('mydiv'). InnerHTML = "move"; } } OR 2) Use function currying to bind the scope at definition time.
I've lifted the bind() method from the Prototype library to illustrate: // declare the global bind() method for all functions Function.prototype. Bind = function(obj) { var method = this, temp = function() { return method. Apply(obj, arguments); }; return temp; } test.
Prototype = { init: function () { addEvent(document, 'mousedown', this. Displaydown); }, displaydown : function(e){ document. GetElementById('mydiv').
InnerHTML = "down"; // note that we bind the method object here addEvent(document, 'mousemove', this.displaymove. Bind(this)); }, displaymove : function(e){ document. GetElementById('mydiv').
InnerHTML = "move"; } } The important change here is: this.displaymove. Bind(this) which basically says, "when you call displaymove(), re-scope the keyword 'this' to the original scope context instead of the current Event object.
Johnvey Thanks! I think I need to do as the 2nd method. But I could not bind().
An error comes up at this step. I'm using IE.6. Is this the reason?
– unigogo May 5 '09 at 6:05 IE6 shouldn't be the problem. What is the error that you're getting? As a larger question, what is it that you're trying to do?
Calling addEvent() from inside another event handler is not good because you'll end up attaching the same handler over and over. – johnvey May 5 '09 at 7:16 johnvey, I finally found the error on your code. AddEvent(document, 'mousedown', this.
Displaydown); should be also binded. Or this in the scope of mousedown is window. I just worked on a slider for easy loading.Pagecolumn.Com/webparts/sliders.
Htm – unigogo May 11 '09 at 7:49.
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.