473,396 Members | 1,968 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

assigning a method to a var

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?

Jul 23 '05 #1
4 1379
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
Jul 23 '05 #2
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.
Jul 23 '05 #3
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
Jul 23 '05 #4
"dr.bob" <dr**********@gmail.com> wrote in message
news:d7**********@h1.uw.edu.pl...
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 <gw*****@agricoreunited.com>
comp.lang.javascript FAQ - http://jibbering.com/faq
Jul 23 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

37
by: Dave | last post by:
Hello all, Please consider the code below. It is representative of a problem I am having. foo_t needs to contain a bar_t which is a class without a copy constructor or operator=. It is not...
1
by: Dave | last post by:
I am trying to create a function in SQL 2000. Im rusty on function syntax and need to also assign some variables for use within this function. Example: create function blah (@param1 int) ...
4
by: lkrubner | last post by:
I'm reading an essay, I think one of Crockford's, and it has this example in it: function Demo() { } Demo.prototype = new Ancestor(); Demo.prototype.foo = function () { } ; Does Ancestor now...
14
by: Eric Bantock | last post by:
Very basic question I'm afraid. Once an array has been declared, is there a less tedious way of assigning values to its members than the following: myarray=8; myarray=3; myarray=4; myarray=0;...
3
by: JJ_377 | last post by:
I made a user control to gather usa address information and would like to know why the following doesn't work (I am assigning the valid of textbox in a second instance of the control from the first...
15
by: Iced Crow | last post by:
In C# I know that you can use delegates to assing multiple addresses of sub and functions to a delegate and have it fire multiple procedures... How do I do this in VB? I only know of assigning...
11
by: JohnR | last post by:
I'm trying to find a way to create a variable of a given type at runtime where I won't know the type until it actually executes. For example, dim x as object = "hi" x is declared as an object...
20
by: weston | last post by:
I've got a piece of code where, for all the world, it looks like this fails in IE 6: hometab = document.getElementById('hometab'); but this succeeds: hometabemt =...
1
by: weston | last post by:
So, the following apparently works as a method of grafting a method onto an object (using the squarefree JS shell): obj = { x: 1, inc: null } result obj.inc = function () { this.x++ } result...
1
by: The Pythonista | last post by:
I've been wondering for a while about whether assigning to __class__ is bad form or not. Specifically, I mean doing so when some other method of implementing the functionality you're after is...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.