tags:

views:

77

answers:

3

I mean a wrap function like this:

function $(id) { return document.getElementById(id); }

but in some code like this:

oDiv1 = $("postInfoDiv");
oDiv2 = document.getElementById("postInfoDiv");
alert(oDiv1 == oDiv2);                     // return false
alert(oDiv1.style);                            // error
alert(oDiv2.style);                            // correct
alert(document.getElementById("postInfoDiv").style); // correct

I got strange results as the comments imply.
I thought the first alert should return the true since they are the same dom object.
I thought the second alert should alert something like "object" or "CSS StyleDeclaration" but not "defined".

So what are the problems? Have you ever met this kind of problems?

thanks.

+2  A: 

Your $ function is probably being overridden, potentially by a framework.

You should try doing alert( oDiv1.nodeType ) to see if it's a DOM element. alert( oDiv1.length ) to see if it's an empty array because you may be using jQuery on the same page which overrides your $ function.

oDiv1 may be an array-like object containing that item if jQuery is included. oDiv2 is an actual DOM reference. You probably need to compare oDiv1[0] to oDiv1, in which you reference the first element in the array which points to the actual dom element to make a fair comparison.

function $(id){return document.getElementById(id)}
$('content') == document.getElementById('content')
true

The custom $ function will work perfectly but if you're using a framework function it will return that array-like object instead of the DOM element.

You can also rename your function to something like function getID which would be unique and not conflict with framework $s.

meder
Did you look at his $ function in the question? No array there.
epascarello
Why would `oDiv1` be an array?
RoToRa
He's most likely accidentally using another `$` function instead of the one he defined. Maybe he has Prototype/jQuery on the same page. Otherwise it wouldn't equate to false.
meder
@meder: Yes, I have jQuery on the same page. How did you know it? But I have searched the jQuery source code but could not find a function name $(),though I am sure jQuery has one.
Jichao
jQuery IS defined as `window.$`. You can use `jQuery.noConflict` to make jQuery not use `$` but it would break your code. If you try this in a page without jQuery, it'll work perfectly. Soo, I suggest renaming your function or just not using your custom `$` because you're already using jQuery. There is no difference between your function and just using gEBI.
meder
@jcyang: By default `$ == jQuery`.
KennyTM
+1 Looks like you nailed it. Good call.
patrick dw
@meder: I changed the '$', and it works. It is the conflict between this $ function and jQuery that produce these strange results. thanks.
Jichao
A: 

My main concern with this is that it will confuse the heck out of someone the first time they read your code, especially if they are used to coding with a JavaScript framework such as jQuery.

For this reason alone I recommend you do not use this particular syntax for your wrap function.

Justin Ethier
Asking the OP not to use a convenient shorthand because it's common in other libraries doesn't seem like a reasonable answer to his question. Using that logic jQuery shouldn't use $ because Prototype does, or vice versa. You can't assume a JS Project uses a specific library because of a $() function.
g.d.d.c
The accepted answer essentially says the same thing I am - to rename his function because it is conflicting with jQuery.
Justin Ethier
i was going to say the exact same thing.
Patricia
A: 

BTW note that even when jQuery is not loaded, Firebug provides its own $ function, which may participate to confusion.

greg
Oh, I debuged this page with Firebug too.
Jichao