I would like to know what is difference in writing in below 2 ways :  Approach 1:  var onCallback = function(result) {      alert('way1'); };  function1(onCallback);   Approach 2:  function2(function(result) {     alert('way2'); });
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?
41914
Consider:  int testfunc1 (const int a) {   return a; }  int testfunc2 (int const a) {   return a; }   Are these two functions the same in every aspect or is there a difference?  I'm interested in an answer for the C language, but if there is something interesting in the C++ language, I'd like to know as well.
8344
I'm working with a bit of html and Javascript code that I've taken over from someone else. The page reloads a table of data (via an asynchronous request) every ten seconds, and then re-builds the table using some DOM code. The code in question looks something like this:  var blah = xmlres.getElementsByTagName('blah'); for(var i = 0; i &lt; blah.length; i++) {     var td = document.createElement('td');     var select = document.createElement('select');     select.setAttribute("...", "...");     select.onchange = function() {         onStatusChanged(select, callid, anotherid);     };     td.appendChild(select); }   When the onchange event is fired for a &lt;select&gt; element however, it seems like the same values are being passed to the onStatusChanged() method for every &lt;select&gt; in the table (I've verified that in each iteration of the loop, callid and anotherid are being given new, distinct values).   I suspect this is occuring because of the nature of how I am setting the event handler, with the select.onchange = function() syntax. If I understand how this is working correctly, this syntax sets a closure for the onchange event to be a function which refers to these two references, which end up having a final value of whatever they are set to on the last iteration of the loop. When the event fires, the value referenced by callid and anotherid is the value set in the last iteration, not the value set at the individual iteration.  Is there a way that I can copy the value of the parameters I am passing to onStatusChanged()?  I've changed the title to better reflect the question and the accepted answer.
85892
I have seen people use both forms below. Which is correct? If both are, in which situation is each better used?  I am a software engineer based in New York. I am a software engineer based out of New York.
56680
I've seen both the blocks of code in use several different times, personally I have always used the first but my question is: is there a functional difference, and if there is what is it?  while (condition is true ) {     // do something }  do {     // do something } while ( condition is true);   I will be applying this to PHP but I assume this is fairly language agnostic.
26511
I’m looking for a trick about this. I know how to call a dynamic, arbitrary function in JavaScript, passing specific parameters, something like this:  function mainfunc(func, par1, par2){     window[func](par1, par2); }  function calledfunc(par1, par2){     // Do stuff here }  mainfunc('calledfunc', 'hello', 'bye');   I know how to pass optional, unlimited parameters using the arguments collection inside mainfunc, but, I can’t figure how to send an arbitrary number of parameters to mainfunc to be sent to calledfunc dynamically; how can I accomplish something like this, but with any number of optional arguments (not using that ugly if–else)?  function mainfunc(func){     if(arguments.length == 3)         window[func](arguments[1], arguments[2]);     else if(arguments.length == 4)         window[func](arguments[1], arguments[2], arguments[3]);     else if(arguments.length == 5)         window[func](arguments[1], arguments[2], arguments[3], arguments[4]); }  function calledfunc1(par1, par2){     // Do stuff here }  function calledfunc2(par1, par2, par3){     // Do stuff here }  mainfunc('calledfunc1', 'hello', 'bye'); mainfunc('calledfunc2', 'hello', 'bye', 'goodbye');
66401
I'd like to know if someone has a better word than , for both creating and updating something.  Context: I'm a software developer and I'm trying to think of a clever way to name the function that will initially create an object, and if it's already created, update it.
30163
What is the difference between this:  void MyMethod(IMyInterface value) {     //... }   and this:  void MyMethod&lt;T&gt;(T value) where T : IMyInterface {     //... }
12628
Is there a benefit to using one over the other? In Python 2, they both seem to return the same results:  &gt;&gt;&gt; 6/3 2 &gt;&gt;&gt; 6//3 2
29305
What does it mean to extend a function? Can someone please give an example?  Thanks in advance!
106791
I am finding an example to clearly differentiate and demonstrate the use of these two words. At the moment, I am relying more on my feelings to decide which word to use. Say I am writing an article that talks about a solution or framework, should I say:   It is a general solution to kill computer virus. It is a generic solution to kill computer virus.   I want to show that the framework is normal, does it job, nothing fancy and that's about it. If the two sentences above are correct, what are the perceived meaning of each sentence?
41585
Could someone explain? I understand the basic concepts behind them but I often see them used interchangeably and I get confused.   And now that we're here, how do they differ from a regular function?
1086
How would you explain JavaScript closures to someone with a knowledge of the concepts they consist of (for example functions, variables and the like), but does not understand closures themselves?  I have seen  given on Wikipedia, but unfortunately it did not help.
9076
In javascript, when would you want to use this:  (function(){     //Bunch of code... })();   over this:  //Bunch of code...
44431
Could you explain the difference between setting methods in the constructor and through prototype object? The following code shows these two ways of setting the methods - say_hello and say_bye both work fine:  function MessageClass() {   this.say_bye = function() { alert('see ya'); }; }  MessageClass.prototype.say_hello = function() { alert('hello'); };  x = new MessageClass(); x.say_hello(); x.say_bye();
107998
Is it like...  var obj = new Object();  obj.function1 = function(){     //code }   or something like that?
13636
Consider these two function definitions:  void foo() { }  void foo(void) { }   Is there any difference between these two? If not, why is the void argument there? Aesthetic reasons?