...

View Full Version : Dollar sign explanation



hemebond
08-15-2005, 03:55 AM
Could someone please explain this bit of code for me, and perhaps paste of couple of links to tutorials that deal with this sort of thing:
remove: function(element) {
element = $(element);Also, if anyone has experience using the Prototype Javascript Framework (http://prototype.conio.net/), I'd appreciate any help in understanding it.

martin_narg
08-15-2005, 04:21 AM
remove is the referencable name of the function. You can name any javascript objects by using a colon after a name, useful for things like this (loop object naming):


var a, b;
outer_loop:
for(a=0; a<5; a++) {
for(b=0; b<5; b++) {
if(a == 1 && b == 3) {
break outer_loop;
}
}
}

or like this (child object naming):


var Prototype = {
Version: '1.3.1', // Prototype object Version object is string '1.3.1'
emptyFunction: function() {} // Prototype object emptyFunction object is an empty anonymous function
}


The function() bit is just declaring an anonymous function passing an parameter "element". The usage of a single $ sign usually denotes the input of an regular expression - it's an attribute of the RegExp object.
However in this case it's actually a function called $:


function $() {
var elements = new Array();

for (var i = 0; i < arguments.length; i++) {
var element = arguments[i];
if (typeof element == 'string')
element = document.getElementById(element);

if (arguments.length == 1)
return element;

elements.push(element);
}

return elements;
}

This function basically returns the html element object(s) for the id string(s) you pass to the $ function. If you only pass one element id in, then it just returns the one html element object, otherwise if more than one id string is passed in, it returns an array of html element objects.
usage:


var obj = $("myDiv"); // same as doing var obj = document.getElementById("myDiv");
var objs = $("myDiv1", "myDiv2");
// same as: var objs = new Array(document.getElementById("myDiv1"), document.getElementById("myDiv2"));

Hope this helps

m_n

hemebond
08-15-2005, 04:32 AM
The function() bit is just declaring an anonymous function passing an parameter "element". The usage of a single $ sign denotes the input of an regular expression - it's an attribute of the RegExp object.Thanks Martin. I should have removed the remove: as I understand that part, It's just the usage of the $ sign I don't understand. I've never seen it used before. You can get the whole script via the link on my original post.

Update: Awesome, thanks Martin. I've been hunting around all day trying to find out what it means. I now remember seeing the $ function, but I thought it was a reference or something. Bah! Dang obscure code.

martin_narg
08-15-2005, 04:35 AM
cool, pleased to help

gregsometimes
12-29-2008, 12:32 AM
4 years later, in jQuery the dollar sign has a special meaning. There is an article written about it on the following url: Explanation of the Javascript Dollar Sign ($) (http://www.authenticsociety.com/blog/JavaScript_DollarSign)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum