What does a $ in a javascript function name mean? - javascript

GWT outputs codes like this:
function com_mycompany_mywebapp_client_CTest_$$init__Lcom_mycompany_mywebapp_client_CTest_2V(){
this$static}
What does the $ mean, or is it just another character like an underscore? Would this_static mean the same thing?

Nuthin. It's just a character. Think of it as an S with a stick stuck through it.

The dollar sign was added to be used for machine-generated symbols so they wouldn't collide with manually written symbols. But syntactically it's just like underscore, it doesn't mean anything special.
When people discovered they could use just $() as a function name, they started to use it as a shortcut for document.getElementById() and different people extended it in different directions. Now it is often used in libraries and frameworks like Prototype or jQuery which both use $() functions in their own way (that's why you have to use jQuery.noConflict() to use both Prototype and jQuery in the same page). The dollar sign is often used in front of variable names to remember that it's a jQuery object, like this:
var $links = $('a');
But some people don't like it being used that way - eg. see the Code Conventions for the JavaScript Programming Language by Douglas Crockford.

Related

What is the primary purpose of function expression in Module Pattern

I know that Module Pattern is very useful and powerful in Javascript programming.
I recognized that pattern in Eric Miraglia's blog for the first time , but I am wondering one thing.
In other blogs and articles that explain Module Pattern, I notice that their sample codes are slightly different from Eric's article, that is, they use function expression with parenthesis rather than function statement, for example, the article from ben cherry is one of them.
Is there any specific reason to use function expression rather than function statement?
Please explain with easy way, I just entered into Javascript Programming world :)
The Miraglia pattern is the same, defining an anonymous function and executing it. The difference is that in order to use the features of the module, you must have a reference to an instance somewhere. Assigning the module to a global variable (YAHOO.*) is a way to retain the reference at a globally known spot, especially important for frameworks (like YUI).
Sometimes you don't need that reference. For example, if you are writing JavaScript for a web page, you often bind events to functions using selectors (ids / types, etc.) That really removes the need for any global reference to your module function.
Hope that make sense...

Javascript "$" meaning before a variable or namespace

Anybody knows what is the meaning of the $ sign before an object attribute.
For example:
pages.$page
I've seen in some javascript codes and I'm not sure if it using some kind of framework.
I've used something like this (this.$el) to access to cached element in Backbone.
Thanks in advance!
As far as javascript is concerned, $ is just another character at the beginning of a variable name, at the end of a variable name, in the middle or all by itself.
As far as some frameworks are concerned like jQuery, it's a character that the framework uses in a specific way (by convention, not because it means anything special to javascript).
For example, in jQuery $(selector) is one of the main functions in jQuery and as such it is a popular convention to assign the resulting jQuery object to a variable with a $ in it like this:
var resetButton$ = $("#reset");
This convention then indicates to the reader that the variable contains a jQuery object.
Other frameworks also use the $ sign, some in similar ways, some in other ways, but in all cases, it's just another character in javascript, but because it stands out, it's often used as a meaningful convention.
Once you become familiar with one of these types of conventions, it can make code a lot easier to read and your brain can actually recognize the meaning of the code even quicker with common, learned conventions. But, these conventions of naming variables a certain way are completely optional.
It is used to mark an element or object as a jQuery object (usually). It's a perfectly valid variable name though.
Many people use $varName to indicate that it is a jQuery variable/property
var $divs = $('div');
var nonJqueryVar = 'hello';

Javascript: $$ Global Variable Considered Evil?

I am writing a lot of Javascript code these days and I am making good use of JQuery. I am wondering if it is considered evil to create strange global variable names. I know that a lot of Javascript frameworks such as JQuery use the dollar character; $, but that greatly simplifies code as it can do so many things.
I am thinking of creating a $$ global variable in my code which would be defined as below:
function $$(tagName)
{
return $('<' + tagName + ' />');
}
The benefit of this is that my code has (1) abstracted out the logic of creating a new element, (2) made the code more concise, and lastly (3) I can almost create html elements within Javascript as concisely as html itself because JQuery has so many other selectors to chain off:
$$('div').attr( { id : 'myDiv', 'class' : 'MyDivClass' }).append(
$$('ul').append(
$$('li').text('first'),
$$('li').text('second'),
$$('li').text('third')
);
);
Do you believe the above approach of creating $$ is legitimate or would you regard it as a no-no?
This is of course an opinion, but if I was new to your application, I would have to go look up what that function does in order to understand the code. Also, the function is simple enough that other developers would likely not bother using it, so you'd end up with a mix of techniques which is confusing.. And really, is this so bad?
$('<div/>').attr( { id : 'myDiv', 'class' : 'MyDivClass' }).append(
$('<ul/>').append(
$('<li/>').text('first'),
$('<li/>').text('second'),
$('<li/>').text('third')
)
);
I prefer a clear, meaningful, self-documenting name over JQuery style $ any day of the year.
Also I find it confusing that the same name has different meanings depending on the context it is used in. Like $ in JQuery or this in javascript and will avoid using them as much as I can.
EDIT: In my opinion it should be up to the user to decide whether he wants to use a shorthand name for the framework he uses. It requires only one line of code:
var $ = jQuery;
When I look at pages with jQuery in it, all I see is a big mess flooded with dollar signs that do different things in different places. If the library had used meaningful names for the functions it offers it would be a lot easier to decipher other peoples code. Javascript is already difficult and unreadable as a language itself.
Personally I don't think it's adding that much functionality to account for the "namespace pollution", not to mention the possible confusion of people reading your code that aren't familiar with your method.
It's just saving a trivial couple of characters on each invocation of the method, so in that respect it can only be considered a form of syntactic sugar. And the cons outweigh the pros, in my opinion.
It would be good if you take a look at the construction of e.g. jQuery .
It does'nt work with several global variables/functions, there is only one global object required "jQuery" .
Everything else are properties of this one object, so they have minified naming-conflicts.
In the special case of $$ : maybe sometimes you need to work with prototypejs, but you cant, because prototypejs already uses $$.
So my suggestion: create one global object, your own "library" , give it a distinct name, and let your functions/variables be member of this object.
I don't think it's "evil" in general, but if you're going to pop something into the global namespace with a "distinguished" name it might be better if it were something a little more interesting. For your purposes, given your example code, the jQuery micro-template system would probably be a better approach anyway.

Why do I see JavaScript variables prefixed with $?

This is sort of a meta-question. Many snippets of JavaScript I've seen here on SO are named with a dollar sign prefix (for example, $id on the second line of the snippet shown in this question). I'm not referring to jQuery or other libraries. I am well aware that this is valid, but it seems awkward to do when not necessary. Why do people name their variables like this? Is it just familiarity with a server-side language like PHP carrying over into their JavaScript code?
I thought perhaps it was to identify a variable as being a jQuery object, for example when you save the result of a selection to a variable in order to eliminate duplicate selections later on, but I haven't seen any consistent convention.
Syntactically, the dollar sign itself means nothing -- to the interpreter, it's just another character, like _ or q. But a lot of people using jQuery and other similar frameworks will prefix variables that contain a jQuery object with a $ so that they are easily identified, and thus not mixed up with things like integers or strings. You could just as easily adopt the same convention by prefixing such variables with jq_ and it would have the same effect.
In effect, it is a crude sort of Hungarian notation.
I will sometimes prefix a variable name with $ to indicate that it is a jQuery-wrapped element (most often when I'm using $(this) in a function a lot, I will assign that to $this).
Part of where the lack of convention may come from is people copy-pasting code together that uses different conventions, thus producing inconsistent code.
Also, sometimes (rarely I hope) people who program in PHP a lot will put $'s at the beginning of their variable names out of habit.
I suspect the person in that example was just copying the jQuery pattern, or is used to PHP/Perl, without really understanding that it isn't necessary and has no special meaning.
However, I have seen [experienced] programmers use it for variable names that are reserved keywords, such as $class or $this. Or even for globals. It's really a personal preference more than anything, in that case.

Capitalization convention for JavaScript objects

I know this question has no answer, but I'm curious to know what other people think.
In a language like Java, it's a convention to begin classes with capital letters, and objects with lowercase letters. But what about JavaScript, where everything is an object?
I've seen some people suggest capitalizing only objects that are treated as classes; i.e. function objects with a prototype, that are intended to be used with the new operator. Instances of those objects would be lowercased.
That sounds logical. But what do you do about "global objects", where there's only one instance? Most people seem to capitalize these (for example, Math or Ext.History). This intuitively feels right, but it's hard to justify it with a consistent rule.
And what about objects that are used as namespaces? These seem to be all over the map: YUI, Ext.util, jQuery, etc.
Please provide secular rationalizations for your heart-felt religious views.
You can follow this Google JavaScript Style Guide
In general, use functionNamesLikeThis, variableNamesLikeThis, ClassNamesLikeThis, EnumNamesLikeThis, methodNamesLikeThis, and SYMBOLIC_CONSTANTS_LIKE_THIS.
As recommended by Douglas Crockford:
"Constructor functions that must be used with the new prefix should start with a capital letter. JavaScript issues neither a compile-time warning nor a run-time warning if a required new is omitted. Bad things can happen if new is missing, so the capitalization convention is an important defense."
https://www.crockford.com/code.html
The convention is that there is no convention. Do what you want, just be consistent. I suggest follow Java style and ignore whatever convention the library (dojo, Ext, YUI, $, etc) you happen to be using is following.
I agree with the capitalization of functions that define "classes" (air-quotes used) that in turn will be instanciated later using the new operator.
But that's it. Global objects are just global. Name them what you want.
All I would make sure is that they are unique and descriptive enough that they won't be overwritten accidentally by another developer at a later date.

Categories