What is the different between following two methods of defining js function. I have seen this is some code some one given, but could not able to call the function inside of it.   1)   function sum () {     var i, sum = 0;     for (i = 0; i &lt; arguments.length; i += 1) {         sum += arguments[i];     } return sum; };   2)   var sum = function () {     var i, sum = 0;     for (i = 0; i &lt; arguments.length; i += 1) {         sum += arguments[i];     }     return sum; };
647
I've recently started maintaining someone else's JavaScript code. I'm fixing bugs, adding features and also trying to tidy up the code and make it more consistent.  The previous developer used two ways of declaring functions and I can't work out if there is a reason behind it or not.  The two ways are:  var functionOne = function() {     // Some code };     function functionTwo() {     // Some code }   What are the reasons for using these two different methods and what are the pros and cons of each? Is there anything that can be done with one method that can't be done with the other?
29305
What does it mean to extend a function? Can someone please give an example?  Thanks in advance!
588
JavaScript allows functions to be treated as objects--if you first define a variable as a function, you can subsequently add properties to that function.  How do you do the reverse, and add a function to an "object"?  This works:  var foo = function() { return 1; }; foo.baz = "qqqq";   At this point, foo() calls the function, and foo.baz has the value "qqqq".  However, if you do the property assignment part first, how do you subsequently assign a function to the variable?  var bar = { baz: "qqqq" };   What can I do now to arrange for bar.baz to have the value "qqqq" and bar() to call the function?
18490
Can someone provide a simple explanation of methods vs. functions in OOP context?
45206
What is the difference between Object Literals and Array Literals in JavaScript? I know it has something to do with the length method but i don't fully understand it.
9968
I've been making a concerted effort to improve my javascript skills lately by reading as much javascript code as I can. In doing this I've sometimes seen the javascript: prefix appended to the front of event handler attributes in HTML element tags. What's the purpose of this prefix? Basically, is there any appreciable difference between:  onchange="javascript: myFunction(this)"   and  onchange="myFunction(this)"   ?
30163
What is the difference between this:  void MyMethod(IMyInterface value) {     //... }   and this:  void MyMethod&lt;T&gt;(T value) where T : IMyInterface {     //... }
66573
I know that in JavaScript, creating a for loop like this: for(int i = 0; i &lt; arr.length; i++) is costly as it computes the array length each time. Is this behavior costly in c# for lists and arrays as well. Or at compile-time is it optimized?  Also what about other languages such as Java, how is this handled?
36511
I understand passing in a function to another function as a callback and having it execute, but I'm not understanding the best implementation to do that. I'm looking for a very basic example, like this:  var myCallBackExample = {     myFirstFunction : function( param1, param2, callback ) {         // Do something with param1 and param2.         if ( arguments.length == 3 ) {             // Execute callback function.             // What is the "best" way to do this?         }     },     mySecondFunction : function() {         myFirstFunction( false, true, function() {             // When this anonymous function is called, execute it.         });     } };   In myFirstFunction, if I do return new callback(), then it works and executes the anonymous function, but that doesn't seem like the correct approach to me.
42746
Should I use exit() or just return statements in main()? Personally I favor the return statements because I feel it's like reading any other function and the flow control when I'm reading the code is smooth (in my opinion). And even if I want to refactor the main() function, having return seems like a better choice than exit().  Does exit() do anything special that return doesn't?
19743
I know that it is defined such that there is no more than one y-value output for any given x-value input, but I"m wondering WHY it is defined that way? Why can't we apply everything we know about functions to equations that have more than one y-value output for any given x-value input?   In short, what is the benefit or reason to define a function this way?  At the same time, a little historical context would also be appreciated (i.e., when the concept originated and why).  Thanks,  Moshe
110764
In the past and with most my current projects I tend to use a for loop like this:  var elements = document.getElementsByTagName('div'); for (var i=0; i&lt;elements.length; i++) {     doSomething(elements[i]); }   I've heard that using a "reverse while" loop is quicker but I have no real way to confirm this:  var elements = document.getElementsByTagName('div'),      length = elements.length;  while(length--) {     doSomething(elements[length]); }   What is considered as best practice when it comes to looping though elements in JavaScript, or any array for that matter?
51606
Possible Duplicate:          This is something I haven't quite figured out yet, but I have been using function(){}() just because my VIM syntax highlight screws up if I add the parenthesis, although I've seen (function(){})() around many times, maybe its an IE thing?  edit:  var singleton = function() {     // code }();  var singleton = (function() {     // code })();
29396
I'm wondering whether there is a difference between these two expressions. I never know which one to use. Google seems to return the same amount of results for both, so I suppose there might be a difference.        What are the alternatives for this project?     What are the alternatives to this project?
52702
As far as I can tell, these two pieces of javascript behave the same way:  Option A:  function myTimeoutFunction() {     doStuff();     setTimeout(myTimeoutFunction, 1000); }  myTimeoutFunction();   Option B:  function myTimeoutFunction() {     doStuff(); }  myTimeoutFunction(); setInterval(myTimeoutFunction, 1000);   Is there any difference between using  and ?
7669
What's the difference between JavaScript and Java?
86079
I recall there is a difference between some methods/properties called directly on the  class, and the identically named methods/properties on the  property. (Might have been the RowCount/Count property for which I read this.) The difference is one of them disregards , and the other respects/uses it.  In this particular case I'm wondering about the difference between  and . I can imagine one of them actually removes all rows, and the other one just marks them as deleted.  So my question is, is there a difference between the two Clear methods, and if so what is the difference?  (Oh, this is for .NET 1.1 btw, in case the semantics changed from one version to another.)