I was just trying function hoisting in js , I have a doubt in below code  // Case 1 var foo = function() {     return 3; }  // Case 2 function foo() {     return 2;     } alert(foo());   Why case1 gets preference over case 2 always? case 2 never overrides case 1  , why?
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?
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');
5812
Why does the  exist in modern languages such as C# and Java? Is it purely a self documenting code feature, or does it serve any actual purpose?  For instance the following example:  Class1 obj = new Class1();  Class1 foo() {     return new Class1(); }   Is as easy to read as the more Pythonesque way of writing it:  Class1 obj = Class1();  Class1 foo() {     return Class1(); }   EDIT:  hit the nail on the head with the clarification of the question: Why did they choose this syntax?
116635
Let's say - $F(x) &gt; G(x)$ for all $x$ in $E$.  So is it true that ?  $$\int F(x)\,dx  &gt; \int G(x)\,dx$$ or I can use this statement only when the two functions are non-negative functions.  Thanks!
29117
So I was playing around the other day just to see exactly how mass assignment works in JavaScript.  First I tried this example in the console:  a = b = {}; a.foo = 'bar'; console.log(b.foo);   The result was "bar" being displayed in an alert. That is fair enough, a and b are really just aliases to the same object. Then I thought, how could I make this example simpler.  a = b = 'foo'; a = 'bar'; console.log(b);   That is pretty much the same thing, isn't it? Well this time, it returns foo not bar as I would expect from the behaviour of the first example.  Why does this happen?  N.B. This example could be simplified even more with the following code:    a = {}; b = a; a.foo = 'bar'; console.log(b.foo);  a = 'foo'; b = a; a = 'bar'; console.log(b);     (I suspect that JavaScript treats primitives such as strings and integers differently to hashes. Hashes return a pointer while "core" primitives return a copy of themselves)
23938
So this is a piecewise defined function I have that I need to talk about:     f(a,b) = \left\{      \begin{array}{lr}        \text{open} &amp; : \text{RMSD}_\text{s-open}\ge6, \text{RMSD}_\text{closed}\ge6\\        \text{closed} &amp; : \text{RMSD}_\text{closed}\le2 \\        \text{semiopen} &amp; : \text{RMSD}_\text{s-open}\le2\\        \text{transition} &amp; :f(a,b)\notin\{\text{open}, \text{closed}, \text{semiopen}\} \\       \end{array}    \right.   So what's the way to align up the colons (conditions) so that it looks all nice and pretty?   Also, if I want to describe transition to be the value of the function when none of the conditions of the previous 3 are satisfied, is the way I wrote it out with f(a,b) not belonging to the set {open,closed,semiopen} a good way of writing it? seems really unprofessional...
9076
In javascript, when would you want to use this:  (function(){     //Bunch of code... })();   over this:  //Bunch of code...
90445
Is there a way to fall through multiple case statements without stating case value: repeatedly?  I know this works:  switch (value) {    case 1:    case 2:    case 3:       // Do some stuff       break;    case 4:    case 5:    case 6:       // Do some different stuff       break;    default:        // Default stuff       break; }   but I'd like to do something like this:  switch (value) {    case 1,2,3:       // Do something       break;    case 4,5,6:       // Do something       break;    default:       // Do the Default       break; }   Is this syntax I'm thinking of from a different language, or am I missing something?
47950
Why don't more mainstream statically typed languages support function/method overloading by return type?  I can't think of any that do.  It seems no less useful or reasonable than supporting overload by parameter type.  How come it's so much less popular?
55901
I'm learning about decision making structures and I came across these codes:  if [ -f ./myfile ] then      cat ./myfile else      cat /home/user/myfile fi   [ -f ./myfile ] &amp;&amp; cat ./myfile || cat /home/user/myfile   Both of them behave the same. Are there any advantages to using one way from the other?
106126
I was debugging something and discovered some strangeness in JavaScript:  alert(1=='') ==&gt; false alert(0=='') ==&gt; true alert(-1=='') ==&gt; false   It would make sense that an implied string comparison that 0 should = '0'. This is true for all non-zero values, but why not for zero?
46850
I'm trying to get a case-insensitive search with two strings in JavaScript working.  Normally it would be like this:  var string="Stackoverflow is the BEST"; var result= string.search(/best/i); alert(result);   The /i flag would be for case-insensitive.  But I need to search for a second string; without the flag it works perfect:  var string="Stackoverflow is the BEST"; var searchstring="best"; var result= string.search(searchstring); alert(result);   If I add the /i flag to the above example it would search for searchstring and not for what is in the variable "searchstring" (next example not working):  var string="Stackoverflow is the BEST"; var searchstring="best"; var result= string.search(/searchstring/i); alert(result);   How can I achieve this?
4188
While I was investigating a problem I had with lexical closures in Javascript code, I came along this problem in Python:  flist = []  for i in xrange(3):     def func(x): return x * i     flist.append(func)  for f in flist:     print f(2)   Note that this example mindfully avoids lambda. It prints "4 4 4", which is surprising. I'd expect "0 2 4".   This equivalent Perl code does it right:  my @flist = ();  foreach my $i (0 .. 2) {     push(@flist, sub {$i * $_[0]}); }  foreach my $f (@flist) {     print $f-&gt;(2), "\n"; }   "0 2 4" is printed.  Can you please explain the difference ?    Update:   The problem is not with i being global. This displays the same behavior:  flist = []  def outer():     for i in xrange(3):         def inner(x): return x * i         flist.append(inner)  outer() #~ print i   # commented because it causes an error  for f in flist:     print f(2)   As the commented line shows, i is unknown at that point. Still, it prints "4 4 4".
111110
I'm a bit confused as to how to get two method to call each other (i.e., have A() call B() and B() call A()). It seems that F# only 'sees' the method after it's been encountered in code, so if it hasn't, it just says value or constructor has not been defined.  Am I missing something very basic here?
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.
26698
function a () {     return "foo"; }  a.b = function () {     return "bar"; }  function c () { }; c.prototype = a;  var d = new c(); d.b(); // returns "bar" d(); // throws exception, d is not a function   Is there some way for d to be a function, and yet still inherit properties from a?
104870
I've been working on this task (see screenshot).     Here's my answer to it so far:  var sum_pairs=function(ints, s){   //your code here    var ptr_1 = 0,       ptr_2 = 0,       i = 0,       j = 0,       len = ints.length,       j_min = len,       arr_sum = [];    for (i; i &lt; len-1; i++){     for (j = i+1; j &lt; len; j++){       if(ints[i] + ints[j] === s){         if (j &lt; j_min){           j_min = j;           arr_sum = [parseInt(ints[i]), parseInt(ints[j])]         }       }     }   }  return ( (arr_sum.length === 0)? undefined : arr_sum );   }   However, while the tests all pass, it throws an STDERR about 'optimising the code':    How might I be able to optimise my code further?