On 27/05/2005 12:15, dr.bob wrote:
why does
var gE = function(s){return document.getElementById(s);};
var foo = gE("foo");
work, while
var gE = document.getElementById;
var foo = gE("foo");
does not?
When you call a function as a method of an object, the this operator for
that function is set to the object. Perhaps an example will help:
var global = this, // References the global object, like window
object = new Object(),
myFunction;
object.myMethod = function() {
alert('this is:\n\nglobal (' + (this == global) + ')\n'
+ 'object (' + (this == object) + ')');
};
myFunction = object.myMethod;
/* Call as method */
object.myMethod(); // object will be true
/* Call as function */
myFunction(); // global will be true
Both function calls result in calling the same function, but you can see
that the value of the this operator is very different in each case.
Some methods will rely on the this operator to reference the object
correctly, otherwise they will fail. That is what you're observing.
Mike
--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.