2

For example, demo_view is an instance of Backbone.View, from this tutorial, this usage is recommended..

demo_view.$el.html()

As I understood, $ in jQuery is a function that should be used with parentheses, e.g. -> $("#id"), but why it can be used in demo_view.$el without a parentheses? Does anyone have ideas about this?

asked Jun 6, 2013 at 5:38
1
  • 2
    You've gotten some good answers, so this is just a fun tidbit to go with them. Paste this into the JavaScript console and see what happens: var π = Math.PI; alert(π); (Note that I'm not recommending that you use characters like this as variable names.) Commented Jun 6, 2013 at 5:45

3 Answers 3

3

It's also a symbol which can be used to name a simple variable, it belongs to the set of allowed characters for naming variables in javascript.

In jQuery, $ is used to name a function (it's a shorthand for the variable jQuery actually) but they could have chosen any letter of the alphabet or any other valid variable name.

answered Jun 6, 2013 at 5:40
Sign up to request clarification or add additional context in comments.

Comments

2

The reason why they use $el is to denote that the el element is a jquery object. It is purely for readability. Appending '$' before a variable identifier is kind of like a notation that denotes that the variable is a jquery object.

answered Jun 6, 2013 at 5:42

Comments

0

Both $ and _ are valid symbols that can be used in variable names and property names. The convention by some develoepers is to use $ to indicate that the property or variable is a jQuery object and not some ordinary object or value. Thus you can readily tell that it is a jQuery object and use jQuery methods without doubt.

answered Jun 6, 2013 at 5:42

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.