Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsGeneralPHPASPPerlColdFusionFlashHTML, CSS, ScriptsBrowsers

Webmaster Forum / HTML, CSS, Scripts / JavaScript / May 2005



Tip: Looking for answers? Try searching our database.

assigning a method to a var

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
dr.bob - 27 May 2005 12:15 GMT
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?
dr.bob - 27 May 2005 12:23 GMT
> Hello :)
>
[quoted text clipped - 9 lines]
>
> 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
Grant Wagner - 31 May 2005 19:09 GMT
>>   var gE = document.getElementById;
>>   var foo = gE("foo");
[quoted text clipped - 4 lines]
> 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/archive/2004/09/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.

Signature

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

Michael Winter - 27 May 2005 12:28 GMT
> why does
>
[quoted text clipped - 7 lines]
>
> 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

Signature

Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.

dr.bob - 27 May 2005 12:39 GMT
> 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:
[quoted text clipped - 22 lines]
>
> Mike

aah thanks :) that was pretty clear (and fast :)

/dr.bob
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.