I have one object   var a = {b:{c:{d:{e:1}}}}   I want to copy that object to another variable b, without passing a reference, so I try spread operator  var b = {...a}   It is not removing reference at a deep level, as I change the value in the object "a" it also changes the value in the object "b"      a.b.c.d.e = 2     console.log(b.b.c.d.e)    it give output 2 instead of 1    var a = {b:{c:{d:{e:1}}}}; var b = {...a}; a.b.c.d.e = 2; console.log(b.b.c.d.e) document.write(b.b.c.d.e)     How can I handle this issue
912
What is the most efficient way to clone a JavaScript object? I've seen obj = eval(uneval(o)); being used, but . I've done things like obj = JSON.parse(JSON.stringify(o)); but question the efficiency.  I've also seen recursive copying functions with various flaws.  I'm surprised no canonical solution exists.
33953
Suppose I have two classes:  public class Student {     public int Id {get; set;}     public string Name {get; set;}     public IList&lt;Course&gt; Courses{ get; set;} }  public class StudentDTO {     public int Id {get; set;}     public string Name {get; set;}     public IList&lt;CourseDTO&gt; Courses{ get; set;} }   I would like to copy value from Student class to StudentDTO class:  var student = new Student(); StudentDTO studentDTO = student;   How can I do that by reflection or other solution?
73942
Can someone explain to me how XOR swapping of two variables with no temp variable works?  void xorSwap (int *x, int *y) {     if (x != y) {         *x ^= *y;         *y ^= *x;         *x ^= *y;     } }   I understand WHAT it does, but can someone walk me through the logic of how it works?
82619
I was reading the answers to this question  and got this doubt while reading the comments.  programmers frequently find it confusing that &quot;this&quot; is a pointer but not a reference. another confusion is why &quot;hello&quot; is not of type std::string but evaluates to a char const* (pointer) (after array to pointer conversion) – Johannes Schaub - litb Dec 22 '08 at 1:56 That only shows that it doesn't use the same conventions as other (later) languages. – le dorfier Dec 22 '08 at 3:35 I'd call the &quot;this&quot; thing a pretty trivial issue though. And oops, thanks for catching a few errors in my examples of undefined behavior. :) Although I don't understand what info about size has to do with anything in the first one. A pointer is simply not allowed to point outside allocated memory – jalf Dec 22 '08 at 4:18 Is this a constant poiner? – yesraaj Dec 22 '08 at 6:35 this can be constant if the method is const int getFoo() const; &lt;- in the scope of getFoo, &quot;this&quot; is constant, and is therefore readonly. This prevents bugs and provides some level of guarantee to the caller that the object won't change. – Doug T. Dec 22 '08 at 16:42 you can't reassign &quot;this&quot;. i.e you cannot do &quot;this = &amp;other;&quot;, because this is an rvalue. but this is of type T*, not of type T const . i.e it's a non-constant pointer. if you are in a const method, then it's a pointer to const. T const . but the pointer itself is nonconst – Johannes Schaub - litb Dec 22 '08 at 17:53 think of &quot;this&quot; like this: #define this (this_ + 0) where the compiler creates &quot;this_&quot; as a pointer to the object and makes &quot;this&quot; a keyword. you can't assign &quot;this&quot; because (this_ + 0) is an rvalue. of course that's not how it is (there is no such macro), but it can help understand it – Johannes Schaub - litb Dec 22 '08 at 17:55  My question is, why is this a pointer a not a reference? Any particular reason for making it a pointer?  Some further arguments why this being a reference would make sense:  Consider Item 1 from More Effective C++ : use references when it is guaranteed that we have a valid object i.e. not a NULL (my interpretation). Furthermore, references are considered safer than pointers (because we can't screw the memory up with a stray pointer). Thirdly, the syntax for accessing references (.) is a little bit nicer and shorter than accessing pointers (-&gt; or (*)).
95563
I can not understand this shift operator (c# reference):  class MainClass1 {  static void Main()     {         int i = 1;         long lg = 1;         Console.WriteLine("0x{0:x}", i &lt;&lt; 1);         Console.WriteLine("0x{0:x}", i &lt;&lt; 33);         Console.WriteLine("0x{0:x}", lg &lt;&lt; 33);     } }  /* Output: 0x2 0x2 0x200000000 */  class MainClass2 {      static void Main()      {          int a = 1000;          a &lt;&lt;= 4;          Console.WriteLine(a);      } }  /* Output: 16000 */
109364
I have a method which takes params object[] such as:  void Foo(params object[] items) {     Console.WriteLine(items[0]); }   When I pass two object arrays to this method, it works fine:  Foo(new object[]{ (object)"1", (object)"2" }, new object[]{ (object)"3", (object)"4" } ); // Output: System.Object[]   But when I pass a single object[], it does not take my object[] as the first param, instead it takes its all elements like I wanted to pass them one by one:  Foo(new object[]{ (object)"1", (object)"2" }); // Output: 1, expected: System.Object[]   How do I pass a single object[] as a first argument to a params array?
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.
14108
Basically, I'm trying to create an object of unique objects, a set. I had the brilliant idea of just using a JavaScript object with objects for the property names. Such as,  set[obj] = true;   This works, up to a point. It works great with string and numbers, but with other objects, they all seem to "hash" to the same value and access the same property. Is there some kind of way I can generate a unique hash value for an object? How do strings and numbers do it, can I override the same behavior?
29529
I am a big fan of letting the compiler do as much work for you as possible.  When writing a simple class the compiler can give you the following for 'free':   A default (empty) constructor A copy constructor A destructor An assignment operator (operator=)   But it cannot seem to give you any comparison operators - such as operator== or operator!=.  For example:  class foo { public:     std::string str_;     int n_; };  foo f1;        // Works foo f2(f1);    // Works foo f3; f3 = f2;       // Works  if (f3 == f2)  // Fails { }  if (f3 != f2)  // Fails { }   Is there a good reason for this?  Why would performing a member-by-member comparison be a problem?  Obviously if the class allocates memory then you'd want to be careful, but for a simple class surely the compiler could do this for you?
27442
I want an object to have the same Transforms (location, rotation, and scale) as another object. How can I copy the Transforms from one object to another?
29559
How do I remove all attributes which are undefined or null in a JavaScript object?  (Question is similar to  for Arrays)
37713
I am trying to create a MATLAB class with a member variable that's being updated as a result of a method invocation, but when I try to change the property within the class it (apperently, from what I understood from MATLAB's memory management) creates a copy of the object and then modifies it, leaving the original object's property untouched.  classdef testprop       properties           numRequests=0;       end       methods           function Request(this, val)               disp(val);               this.numRequests=this.numRequests+1;           end       end   end     .  &gt;&gt; a=testprop; &gt;&gt; a.Request(9); &gt;&gt; a.Request(5);   &gt;&gt; a.numRequests    ans = 0
124250
I am in the process of learning Rust and am still having some issues with borrowing and ownership. I find myself trying to borrow mutable references that are already borrowed as immutable references etc...  I noticed that when I try to iterate over the primes without owning them, I encounter issues trying to modify the composites hash map. I was able to get around this by calling to_owned on the vector. Is this the right way to handle this?  I also see that when I build the code, I get warnings about unused code for warning: struct is never constructed: 'Sieve' and warning: method is never used: 'new', am I constructing them incorrectly?  use std::collections::HashMap;  struct Sieve {     composites: HashMap&lt;u64, Vec&lt;u64&gt;&gt;,     x: u64, }  impl Sieve {     fn new() -&gt; Sieve {         Sieve {             composites: HashMap::new(),             x: 2,         }     } }  impl Iterator for Sieve {     type Item = u64;      fn next(&amp;mut self) -&gt; Option&lt;u64&gt; {         let x = self.x;         self.x = self.x + 1;         match self.composites.get(&amp;x) {             Some(numbers) =&gt; {                 for _num in numbers.to_owned() {                     self.composites                         .entry(x + _num)                         .and_modify(|v| v.push(_num))                         .or_insert(vec![_num]);                 }                 self.composites.remove(&amp;x);                 self.next()             }             None =&gt; {                 self.composites.insert(x * x, vec![x]);                 Some(x)             }         }     } }  fn main() {     let mut sieve = Sieve::new();      println!("{:?}", sieve.next()); // 2     println!("{:?}", sieve.next()); // 3     println!("{:?}", sieve.next()); // 5     println!("{:?}", sieve.next()); // 7 }   .  I , this made it difficult to get feedback on. I have refactored the code to use iterators
27582
I have a set of global counter variables in Javascript:  var counter_0 = 0; var counter_1 = 0; var counter_2 = 0;   etc  I then have a Javascript function that accepts an 'index' number that maps to those global counters. Inside this function, I need to read and write to those global counters using the 'index' value passed to the function.  Example of how I'd like it to work, but of course doesn't work at all:  function process(index) {     // do some processing      // if 'index' == 0, then this would be incrementing the counter_0 global variable     ++counter_+index;       if (counter_+index == 13)     {         // do other stuff     } }   I hope what I'm trying to accomplish is clear. If not I'll try to clarify. Thanks.  EDIT Clarification:  I'm not trying to increment the name of the counter, but rather the value the counter contains.
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?
15653
Possible Duplicate:          I'm looking to make a callable JavaScript object, with an arbitrary prototype chain, but without modifying Function.prototype.  In other words, this has to work:  var o = { x: 5 }; var foo = bar(o); assert(foo() === "Hello World!"); delete foo.x; assert(foo.x === 5);   Without making any globally changes.
70884
I made many copies of an object, then I thought it would've been better if I made them to share the same Data (being an instance of the same original object), I know how to do it one by one, is there a way to do it for all of them at once? I tried Copy Attribute add-on but didn't find what option can do that, or even if this is possible in the first place.