Evan Charlton wrote:
Quote:
Hey all, I'm having some trouble with window.setInterval() within a
custom object/prototype. Here is my code:
>
[code]
function MyClass() {
Why are people so obsessed with classes when it comes to OOP? From the
above I thought you had understood that ECMAScript 3 implementations use
only prototype-based inheritance.
Quote:
// do some junk
// ...
// define methods
this.m_one = function() {
// nothing happens with this line. No alert, no errors
window.setInterval( "this.m_two", 500 );
Why should there? It is merely a not evaluated read access to the
non-existing property `m_two' *of the Global Object*.
Quote:
// "error: this.m_two() is not a function"
window.setInterval( "this.m_two()", 500 );
This is trying to call a method of the Global Object. Since the
identifier cannot be resolved, you get the runtime error.
Quote:
// "error: m_two() is not defined"
window.setInterval( "m_two()", 500 );
This is trying to call an identifier without an explicit object
reference. Since the identifier cannot be resolved through the scope
chain, you get the runtime error.
Quote:
// "error: m_two is not defined"
window.setInterval( "m_two", 500 );
This is trying to access an identifier without an explicit object
reference. Since the identifier cannot be resolved through the scope
chain, you get the runtime error.
As you can see, you cannot use a locally defined name in a string for
the window.setInterval() method (it would be the same with
window.setTimeout()). However, you can pass a Function object
reference, that is a reference to your method:
window.setInterval(this.m_two, 500);
Quote:
// the following one works just fine .
window.setInterval( "timer()", 500 );
Because timer() is a globally declared function, and (so) a method of
the Global Object. "this.timer()" should work as well.
}
Quote:
this.m_two = function() {
alert("m_two");
}
}
[...]
HTH
PointedEars
--
var bugRiddenCrashPronePieceOfJunk = (
navigator.userAgent.indexOf('MSIE 5') != -1
&& navigator.userAgent.indexOf('Mac') != -1
) // Plone, register_function.js:16