Code Poet
Code Poet

Reputation: 11427

Meaning of var $view = $().find('.something)

I've got this javascript code inside an Ember.View class definition:

didInsertElement: function(){
    var view = this;
    var $view = this.$().find('.xxx');
    Ember.run.later($view, function(){
        $view.css(...);
    }, 100);
},

I don't understand the $view = <some jQuery method result> syntax. I've googled to see if $view is a valid syntax for a variable and if it means anything special. From what I can tell, var $view is just declaring a regular javascript variable. The $ is a valid identifier character. So, saying var $view is no different from saying var view.

PS: The this.$() inside of Ember view gives us the jQuery object for the corresponding ember view object.

Upvotes: 1

Views: 252

Answers (3)

CRABOLO
CRABOLO

Reputation: 8793

A lot of people use $ in front of variables that are functions. And don't use $ when the variable is just equal to a string or number. Helps the readability of their code.

Upvotes: 1

user1508519
user1508519

Reputation:

Of course it's a valid character in a variable name, how else would $ (for example, in jQuery) work?

What characters are valid for JavaScript variable names?

An identifier must start with $, _, or any character in the Unicode categories “Uppercase letter (Lu)”, “Lowercase letter (Ll)”, “Titlecase letter (Lt)”, “Modifier letter (Lm)”, “Other letter (Lo)”, or “Letter number (Nl)”.

Upvotes: 0

Arun P Johny
Arun P Johny

Reputation: 388406

Yes it is a valid syntax

From MDN

A JavaScript identifier must start with a letter, underscore (_), or dollar sign ($); subsequent characters can also be digits (0-9). Because JavaScript is case sensitive, letters include the characters "A" through "Z" (uppercase) and the characters "a" through "z" (lowercase).

So yes it is same as saying var view = this.$().find('.xxx');

Upvotes: 3

Related Questions