In the line I have commented below, does the value of the array that has &quot;element&quot; as a value get overridden by array?   let c = [1, 2]; console.log(`c = ${c}`); add(c, 3); console.log(`c = ${c}`);  function add(array, element) {   array = [element]; // Is this Array that has "element" as a value overridden by array }
330
The primitive types (number, string, etc.) are passed by value, but objects are unknown, because they can be both passed-by-value (in case we consider that a variable holding an object is in fact a reference to the object) and passed-by-reference (when we consider that the variable to the object holds the object itself).  Although it doesn't really matter at the end, I want to know what is the correct way to present the arguments passing conventions. Is there an excerpt from JavaScript specification, which defines what should be the semantics regarding this?
31492
How do I enumerate the properties of a JavaScript object?  I actually want to list all the defined variables and their values, but I've learned that defining a variable actually creates a property of the window object.
27143
I use LINQ to Objects instructions on an ordered array.  Which operations shouldn't I do to be sure the order of the array is not changed?
35414
In javascript you can easily create objects and Arrays like so:  var aObject = { foo:'bla', bar:2 }; var anArray = ['foo', 'bar', 2];   Are similar things possible in PHP? I know that you can easily create an array using the array function, that hardly is more work then the javascript syntax, but is there a similar syntax for creating objects? Or should I just use associative arrays?  $anArray = array('foo', 'bar', 2); $anObjectLikeAssociativeArray = array('foo'=&gt;'bla',                                       'bar'=&gt;2);   So to summarize: Does PHP have javascript like object creation or should I just use associative arrays?
5148
I have recently been thinking about the difference between the two ways of defining an array:   int[] array int array[]   Is there a difference?
22625
MyClass[] array; List&lt;MyClass&gt; list;   What are the scenarios when one is preferable over the other? And why?
7953
Can a string be used as array index in C?  Ex: String      Corresponding value "ONE"       1 "TWO"       2 "FIVE"      5 "TEN"       10  When a string in the above list is passed to the function, the function must return the corresponding value indicated above. Can this be achieved by declaring a constant array with string as index  int *x; x["ONE"]  = 1; x["TWO"]  = 2; x["FIVE"] = 5; x["TEN"]  = 5;  return x["string received by the function"];   The above logic does not work as expected; is there a workaround to implement the above logic in order to have a string-indexed array?
80355
I have this struct:  struct Map {     public int Size;      public Map ( int size )     {         this.Size = size;     }      public override string ToString ( )     {         return String.Format ( "Size: {0}", this.Size );     } }   When using array, it works:  Map [ ] arr = new Map [ 4 ] {     new Map(10),     new Map(20),     new Map(30),     new Map(40)};  arr [ 2 ].Size = 0;   But when using List, it doesn't compile:  List&lt;Map&gt; list = new List&lt;Map&gt; ( ) {     new Map(10),     new Map(20),     new Map(30),     new Map(40)};  list [ 2 ].Size = 0;   Why?
122787
Is there a way to print an entire array ([key]=value) without looping over all elements?    Assume I have created an array with some elements:    declare -A array array=([a1]=1 [a2]=2 ... [b1]=bbb ... [f500]=abcdef)   I can print back the entire array with  for i in "${!array[@]}" do echo "${i}=${array[$i]}" done   However, it seems bash already knows how to get all array elements in one "go" - both keys ${!array[@]} and values ${array[@]}.    Is there a way to make bash print this info without the loop?  Edit: typeset -p array does that! However I can't remove both prefix and suffix in a single substitution:  a="$(typeset -p array)" b="${a##*(}" c="${b%% )*}"   Is there a cleaner way to get/print only the key=value portion of the output?
32884
If I define an array in PHP such as (I don't define its size):  $cart = array();   Do I simply add elements to it using the following?  $cart[] = 13; $cart[] = "foo"; $cart[] = obj;   Don't arrays in PHP have an add method, for example, cart.add(13)?
59216
There are two different ways to create an empty object in JavaScript:  var objectA = {} var objectB = new Object()   Is there any difference in how the script engine handles them? Is there any reason to use one over the other?  Similarly it is also possible to create an empty array using different syntax:  var arrayA = [] var arrayB = new Array()
872
In Java, arrays don't override toString(), so if you try to print one directly, you get the className + '@' + the hex of the  of the array, as defined by Object.toString():  int[] intArray = new int[] {1, 2, 3, 4, 5}; System.out.println(intArray);     // prints something like '[I@3343c8b3'   But usually, we'd actually want something more like [1, 2, 3, 4, 5]. What's the simplest way of doing that? Here are some example inputs and outputs:  // Array of primitives: int[] intArray = new int[] {1, 2, 3, 4, 5}; //output: [1, 2, 3, 4, 5]  // Array of object references: String[] strArray = new String[] {"John", "Mary", "Bob"}; //output: [John, Mary, Bob]
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?
44995
I had this question earlier and it was concluded it was a bug in 5.2.5.  Well, it's still broken in 5.2.6, at least for me:  Please let me know if it is broken or works for you:  $obj = new stdClass(); $obj-&gt;{"foo"} = "bar"; $obj-&gt;{"0"} = "zero"; $arr = (array)$obj;  //foo -- bar //0 --    {error: undefined index} foreach ($arr as $key=&gt;$value){     echo "$key -- " . $arr[$key] . "\n"; }   Any ways to "fix" the array after it has been cast from a stdClass?
17976
I'm using C++ to understand how exactly pointers work. I have this piece of code using arrays, which I'm using just to understand how the equivalent works with pointers.  int main() {         int arr[10] = {1,2,3};         char arr2[10] = {'c','i','a','o','\0'};     cout &lt;&lt; arr &lt;&lt; endl;     cout &lt;&lt; arr2 &lt;&lt; endl; }   However when I run this, arr outputs the address of the first element of the array of ints (as expected) but arr2 doesn't output the address of the first element of the array of chars; it actually prints "ciao".  What is it that I'm missing or that I haven't learned yet about this?
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)
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".