some times in typescript when i pass a variable as a parameter in a function and modify it in the function the original variable also modify can any one explain to me what's happening let mybrew :any[] = [     {         id:5,         materials:[-1,-1,-1,-1],         price:16         },  ] console.log(mybrew); function bestBrew(abrow:any[]){     abrow.forEach(         e=&gt;{             for (let i = 0; i &lt; e.materials.length; i++) {             e.materials[0] += e.materials[i];             }         }     ) } bestBrew(mybrew); console.log(mybrew);  output [ { id: 5, materials: [ -1, -1, -1, -1 ], price: 16 } ] [ { id: 5, materials: [ -5, -1, -1, -1 ], price: 16 } ]
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?
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?
89137
What would be better practice when giving a function the original variable to work with:  unsigned long x = 4;  void func1(unsigned long&amp; val) {      val = 5;             } func1(x);   or:  void func2(unsigned long* val) {      *val = 5; } func2(&amp;x);   IOW: Is there any reason to pick one over another?
21847
Given a function:  function x(arg) { return 30; }   You can call it two ways:  result = x(4); result = new x(4);   The first returns 30, the second returns an object.  How can you detect which way the function was called inside the function itself?  Whatever your solution is, it must work with the following invocation as well:  var Z = new x();  Z.lolol = x;  Z.lolol();   All the solutions currently think the Z.lolol() is calling it as a constructor.
51349
Can I safely omit quotes on the right side of a local assignment?  function foo {     local myvar=${bar}     stuff() }   I'm mainly interested in bash, but any info on corner cases in other shells are welcome.
7923
template&lt;typename T, size_t n&gt; size_t array_size(const T (&amp;)[n]) {     return n; }   The part that I don't get is the parameters for this template function. What happens with the array when I pass it through there that gives n as the number of elements in the array?
13251
Do you think there is a big difference in for...in and for loops? What kind of "for" do you prefer to use and why?  Let's say we have an array of associative arrays:  var myArray = [{'key': 'value'}, {'key': 'value1'}];   So we can iterate:  for (var i = 0; i &lt; myArray.length; i++)   And:  for (var i in myArray)   I don't see a big difference. Are there any performance issues?
4059
The situation is somewhat like-  var someVar = some_other_function(); someObj.addEventListener("click", function(){     some_function(someVar); }, false);   The problem is that the value of someVar is not visible inside the listener function of the addEventListener, where it is probably being treated as a new variable.
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?
3298
After discussion with colleagues regarding the use of the 'var' keyword in C# 3 I wondered what people's opinions were on the appropriate uses of type inference via var?  For example I rather lazily used var in questionable circumstances, e.g.:-  foreach(var item in someList) { // ... } // Type of 'item' not clear. var something = someObject.SomeProperty; // Type of 'something' not clear. var something = someMethod(); // Type of 'something' not clear.   More legitimate uses of var are as follows:-  var l = new List&lt;string&gt;(); // Obvious what l will be. var s = new SomeClass(); // Obvious what s will be.   Interestingly LINQ seems to be a bit of a grey area, e.g.:-  var results = from r in dataContext.SomeTable               select r; // Not *entirely clear* what results will be here.   It's clear what results will be in that it will be a type which implements IEnumerable, however it isn't entirely obvious in the same way a var declaring a new object is.  It's even worse when it comes to LINQ to objects, e.g.:-  var results = from item in someList               where item != 3               select item;   This is no better than the equivilent foreach(var item in someList) { // ... } equivilent.  There is a real concern about type safety here - for example if we were to place the results of that query into an overloaded method that accepted IEnumerable&lt;int&gt; and IEnumerable&lt;double&gt; the caller might inadvertently pass in the wrong type.  var does maintain strong typing but the question is really whether it's dangerous for the type to not be immediately apparent on definition, something which is magnified when overloads mean compiler errors might not be issued when you unintentionally pass the wrong type to a method.
5893
What is the difference between    a parameter passed by reference a parameter passed by value?   Could you give me some examples, please?
39147
is this allowed in java:  for(int i=0;i&lt;5;i++){   final int myFinalVariable = i; }   The keyword of my question is final. Is it allowed to do a final variable that changes with every run of the loop? I was wondering this because final says that you can't change the value of the variable (calling only myFinalVariable = i), but i'm redefining the whole variable with final int.   Are they two completely different variables just with the same name - with the variable from the previous run of the loop already heading down the road to the garbage collector?
51358
How do you decide between passing arguments to a method versus simply declaring them as object instance variables that are visible to all of the object's methods? I prefer keeping instance variables in a list at the end of the Class, but this list gets longer as my program grows. I figure if a variable is passed often enough it should just be visible to all methods that need it, but then I wonder, &quot;if everything is public there will be no need for passing anything at all!&quot;
3385
In the two following snippets, is the first one safe or must you do the second one?  By safe I mean is each thread guaranteed to call the method on the Foo from the same loop iteration in which the thread was created?  Or must you copy the reference to a new variable "local" to each iteration of the loop?  var threads = new List&lt;Thread&gt;(); foreach (Foo f in ListOfFoo) {           Thread thread = new Thread(() =&gt; f.DoSomething());     threads.Add(thread);     thread.Start(); }   -  var threads = new List&lt;Thread&gt;(); foreach (Foo f in ListOfFoo) {           Foo f2 = f;     Thread thread = new Thread(() =&gt; f2.DoSomething());     threads.Add(thread);     thread.Start(); }   Update: As pointed out in Jon Skeet's answer, this doesn't have anything specifically to do with threading.
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?
56347
The  keyword does away with the need for an explicit type declaration and I have read with interest the  of when it might be appropriate.  I have also read about (but not used)  which seems to take things a step further by making it .  With Boo, both the type and the declaration can be implied.  Which leads me to wonder, why did the C# language designers bother to include a var keyword at all?    Update: Yes, var supports Anonymous types, but anonymous types by themselves do not necessitate the var keyword...  var anon = new { Name = "Terry", Age = 34 };   versus  anon = new { Name = "Terry", Age = 34 };
76186
Once it is compiled, is there a difference between:  delegate { x = 0; }   and  () =&gt; { x = 0 }   ?