Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Perfectly reasonable question. :-) In JavaScript, you don't necessarily have to call a function with as many args as it defines, and you don't have to define as many args as you may get called with. Which can be confusing if you're used to more constrained environments. :-)</p> <p>Answering specifics:</p> <blockquote> <p>1) What does function(msg){ServiceSucceeded(msg)} mean?</p> </blockquote> <p>It defines a function (an anonymous one) that accepts one named argument (<code>msg</code>) and calls <code>ServiceSucceded</code> passing in that arg. jQuery will call the function with the three arguments defined by the jQuery documentation for the <code>success</code> function, but this particular <code>success</code> function is only using the first of those (<code>data</code>). More about named functions vs. anonymous functions <a href="http://blog.niftysnippets.org/2010/03/anonymouses-anonymous.html">here</a>.</p> <blockquote> <p>2) What is 'msg' in this context?</p> </blockquote> <p>The first argument to the function. jQuery's docs call this first argument <code>data</code>, but you can call it whatever you like.</p> <blockquote> <p>3) How on earth am I meant to know how to structure the method sugnature for sucess?</p> </blockquote> <p>You did the right thing, it's in the jQuery documentation.</p> <p>This thing about function arguments can be confusing, so let's do some examples:</p> <pre><code>function foo(arg) { alert(arg); } </code></pre> <p>That's perfectly clear, I'm defining a function called <code>foo</code> that takes a single named argument, <code>arg</code>. And thus:</p> <pre><code>foo("Hi there"); // alerts "Hi there" </code></pre> <p>But I can also do this:</p> <pre><code>foo(); // alerts "undefined" </code></pre> <p>There, I didn't give any arguments for <code>foo</code>, and so within <code>foo</code>, <code>arg</code> is undefined.</p> <p>I can also do this:</p> <pre><code>foo("Hi there", "again"); // alerts "Hi there" </code></pre> <p>I'm calling <code>foo</code> with <strong>two</strong> arguments, but <code>foo</code> only makes use of one of them.</p> <p>I could define <code>foo</code> to use as many arguments as you pass in:</p> <pre><code>function foo() { var index; for (index = 0; index &lt; arguments.length; ++index) { alert(arguments[index]); } } </code></pre> <p><code>arguments</code> is an automatic thing all functions have, which is a pseudo-array (it's not really an <code>Array</code>) of the actual arguments the function was called with. And so:</p> <pre><code>foo("Hi there", "again"); // alerts "Hi there", and then alerts "again" </code></pre> <p>You can even mix named and unnamed arguments:</p> <pre><code>function foo(arg) { var index; alert(arg); for (index = 1; index &lt; arguments.length; ++index) { alert("[" + arguments[index] + "]"); } } </code></pre> <p>So now</p> <pre><code>foo("Hi there", "again"); // alerts "Hi there" and then alerts "[again]" </code></pre> <p>Note the <code>[]</code> around the second alert, because I started looping with index <code>1</code> rather than zero.</p> <p><code>arguments</code> and named args are connected:</p> <pre><code>function foo(arg) { alert("arg = " + arg); alert("arguments[0] = " + arguments[0]); arg = "Updated"; alert("arg = " + arg); alert("arguments[0] = " + arguments[0]); } </code></pre> <p>If I do <code>foo("Hi");</code>, that shows these alerts:</p> <pre>arg = Hi arguments[0] = Hi arg = Updated arguments[0] = Updated</pre> <p>(It goes the other way, too, if you update <code>arguments[0]</code>.)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload