Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > assigning a method to a var

Reply
Thread Tools

assigning a method to a var

 
 
dr.bob
Guest
Posts: n/a
 
      05-27-2005
Hello

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?

 
Reply With Quote
 
 
 
 
dr.bob
Guest
Posts: n/a
 
      05-27-2005
dr.bob wrote:

> Hello
>
> 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?


well, read this the other way around it's no surprise to me
that the former works, but i expected the latter to work as well.
what am i getting wrong?

/dr.bob
 
Reply With Quote
 
 
 
 
Michael Winter
Guest
Posts: n/a
 
      05-27-2005
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.
 
Reply With Quote
 
dr.bob
Guest
Posts: n/a
 
      05-27-2005
Michael Winter wrote:

> 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
>


aah thanks that was pretty clear (and fast

/dr.bob
 
Reply With Quote
 
Grant Wagner
Guest
Posts: n/a
 
      05-31-2005
"dr.bob" <dr.bobik+> wrote in message
news:d76vvp$9er$...
>> var gE = document.getElementById;
>> var foo = gE("foo");
>>
>> does not?

>
> well, read this the other way around it's no surprise to me
> that the former works, but i expected the latter to work as well.
> what am i getting wrong?


Actually, the latter does work in Internet Explorer.

An explanation of why is available at <url:
http://blogs.msdn.com/ericlippert/ar...20/231852.aspx />

So here's what happens when you say foo = document.write; First,
JScript attempts to resolve "document". It can't find a local or global
variable called that, so it asks the global window object for the
document property. IE gives back the document object. JScript then
asks the document object to give back the value of the write property.
IE creates an object which has a default method. The default method
calls the write function, but no one calls the method yet -- we just
have an object which, when invoked, will call the mehtod. JScript
assigns the object to foo. Then when you call foo("hello"); JScript
invokes the default method on the object, which calls the write method.

--
Grant Wagner <>
comp.lang.javascript FAQ - http://jibbering.com/faq


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
assigning js file source to var Tom de Neef Javascript 0 04-01-2008 05:20 PM
IE assigning js var to html element? dave Javascript 0 04-19-2007 07:56 PM
Assigning methods to objects, and assigning onreadystatechange to an XMLHttpRequest -- an inconsistency? weston Javascript 1 09-22-2006 09:33 AM
Assigning a variable to "document.getElementById('VAR').innerHTML" adamrfrench@hotmail.com Javascript 6 10-20-2005 03:37 AM
Threads.. Session var lost, App var ok Alvin Bruney ASP .Net 1 12-02-2003 01:56 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57