1

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.

Code Poet
  • 11,227
  • 19
  • 64
  • 97

3 Answers3

3

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');

Arun P Johny
  • 384,651
  • 66
  • 527
  • 531
1

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.

CRABOLO
  • 8,605
  • 39
  • 41
  • 68
  • I think this is a good point. Although, in my case $ is being used to indicate, perhaps, that we're dealing with a jQuery object inside of an ember view. – Code Poet Dec 12 '13 at 19:56
0

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)”.

Community
  • 1
  • 1