-7

I am learning jQuery. The code snippet as below. I don't understand the syntax of the last statement.

  • Why there's no .?

  • what does the [] mean?

  • BTW, what's special with the $?

    /* Code provided for context, irrelevant to the actual question.
      $('#executeButton').click(function(){
      $('body').addClass('done');
      $('.done #controls :radio').attr('disabled',true);
      var sources$ = $('#sourcePane input:checked~img');
      if ($('[name=clone]:checked').val()=='yes') sources$ = sources$.clone();
      var targets$ = $('#targetPane input:checkbox:checked').parents('.target');
      var operation = $('[name=operations]:checked').val();*/
      targets$[operation](sources$); //<=== HERE
    /*});*/
    

    And some more context:

        <div>
          <label>Operation:</label><br/>
          <input type="radio" name="operations" value="append" checked="checked"/> append
          <input type="radio" name="operations" value="prepend"/> prepend
          <input type="radio" name="operations" value="before"/> before
          <input type="radio" name="operations" value="after"/> after
        </div>
    
smwikipedia
  • 52,824
  • 76
  • 267
  • 432
  • 2
    The variable name can also contain `$`. – Bhojendra Rauniyar Nov 30 '15 at 09:06
  • `targets$` is a variable name having `$`. – Rajesh Kumar Nov 30 '15 at 09:08
  • The variable names most likely have had a `$` put in it to indicate that the variable contains a jQuery object. – Marc Harry Nov 30 '15 at 09:09
  • 1
    Let's see this line: `targets$[operation](sources$);` The `targets$` and `sources$` are simply variables. `$` is a valid character that can be used anywhere in the variable, like how you can use `S`. Changing them to `targetsS` or `sourcesS` mean the same. The `targets$[operation]` is an array index. The `targets$` is already an array that selects all the checked checkbox containing `.target`. – Praveen Kumar Purushothaman Nov 30 '15 at 09:10
  • window.jQuery = window.$ = jQuery; in short its a alias to call jQuery function on the other hand , targets$ is just fancy variable name because you can have special character in your variable names it doesnot realates to jquery function – suraj rawat Nov 30 '15 at 09:12
  • Nothing weird here. That's ordinary jQuery, "$" sign can be used in variables like any other character, as long as it's not the first one. See [here](http://jsfiddle.net/yahavbr/b2La2Lfr/) for example – Shadow The Vaccinated Wizard Nov 30 '15 at 09:12
  • May be this one. http://stackoverflow.com/questions/17189642/difference-between-using-bracket-and-dot-notation – Bhojendra Rauniyar Nov 30 '15 at 09:15

2 Answers2

3

what does the [] mean?

[] is a special javascript property access notation . imagine if i have an ojbect like:

var o =  {
   "abc.xyz" : 5
};

I can't say o.abc.xyz , that's where [] is handy o['abc.xyz'].

What's special with the $?

$ is often used as a naming convention with jquery for examle:

var $this = $(this);
Ramanlfc
  • 7,900
  • 1
  • 14
  • 24
  • I only see a wrapset method invoked with a `.` like `$(*).append(...)`. I didn't know that it can be called like this `$(*)[append](...)`. – smwikipedia Nov 30 '15 at 09:19
  • @smwikipedia: in `$(*)[append](...)`, `append` is a variable name. This works, for example: `var a = "append"; $("*")[a]()`. The variable should contain the function's _name_. – Cerbrus Nov 30 '15 at 09:21
  • @Cerbrus I guess you mean `$('*')['append'](...)` or `var a = 'append'; $('*')[a](...)`. – smwikipedia Nov 30 '15 at 09:25
  • 1
    `[a]`, indeed. Whoops. – Cerbrus Nov 30 '15 at 09:26
  • My confusion is not caused by something like `$('[name=x]')`. I am just not fully *adjusted* to the syntax to invoke a method as a property with `[]`. Well I think that's forgivable after only 2 day's crash learning of jQuery without any JS experience. This answer catches my pain point. At the price of so many down-votes, I guess it won't be easily forgotten. :) – smwikipedia Nov 30 '15 at 09:35
1

Let's see this line:

targets$[operation](sources$);

The targets$ and sources$ are simply variables. $ is a valid character that can be used anywhere in the variable, like how you can use S. Changing them to targetsS or sourcesS mean the same.

The targets$[operation] is an array index. The targets$ is already an array that selects all the checked checkbox containing .target.

Praveen Kumar Purushothaman
  • 154,660
  • 22
  • 177
  • 226