I want to make a "hard copy" of the object that is assigned to this.state and change the copy in the sayHello method. When I use Object.assign it changes this.state. How can I fix this? Or......can I ?  ~~~~~~  class App {     constructor() {         this.state = {             items: []         }      }      sayHello() {          //______I want to clone this.state so I can modify the copy____         //______I use Object.assign()__________________________________          let objCopy = Object.assign({}, this.state)          //_____I change the copy_______________________________________         objCopy.items.push({ text: "hi there" })          //_____But the original was changed...nooooooooo!______________          console.log(this.state);  // Changed!  { items: [ { text: 'hi there' } ] }       }  }  let myApp = new App()  myApp.sayHello();
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.
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;
2464
I'm creating side skirts for a truck and I am duplicating the frames I want. The problem is it's making the duplicated object into another object. How do I put it into the same one?
1924
I want to make a button that changes the active poselib to another one from the list, a specific one. I look in the console and can't see any operator. any ideas?
75138
In Delphi, why does the Assigned() function still return True after I call the destructor?  The below example code will write "sl is still assigned" to the console.  However, I can call FreeAndNil(sl); and it won't be assigned.  I've been programming in Delphi for a while, but this never made sense to me.  Can someone explain?  program Project1; {$APPTYPE CONSOLE} uses SysUtils, Classes;  var   sl : TStringList;  begin   sl := TStringList.Create;   sl.Free;   if Assigned(sl) then     WriteLn('sl is still assigned')   else     WriteLn('sl is not assigned'); end.   I tried comparing the VCL operations... FreeAndNil is short and sweet and makes sense:  procedure FreeAndNil(var Obj); var   P: TObject; begin   P := TObject(Obj);   TObject(Obj) := nil;  // clear the reference before destroying the object   P.Free; end;   But TObject.Free is in mysterious assembler, which I don't understand:  procedure TObject.Free; asm         TEST    EAX,EAX         JE      @@exit         MOV     ECX,[EAX]         MOV     DL,1         CALL    dword ptr [ECX].vmtDestroy @@exit: end;
34277
I need to create an edit component for an SObject from which I do not know the objectApiName at design time. I only know the Id of the SObject. objectApiName could be any valid SObject-name.  Now I want to use lightning:inputField   This needs to be wrapped by lightning:recordEditForm  In case I hardcode it to "Order", provide a vaild Order Id and let the controller load the order and assign it to the Parent-attribute, this works flawlessly:  &lt;aura:attribute name="recordId" type="String" default="" /&gt; &lt;aura:attribute name="Parent" type="SObject" default="{}" /&gt; &lt;aura:attribute name="ParentType" type="String" default="" /&gt; &lt;b&gt;{!v.ParentType}({!v.recordId})&lt;/b&gt; &lt;br/&gt; &lt;lightning:recordEditForm recordId="{!v.recordId}"  objectApiName="Order"&gt;     &lt;lightning:messages /&gt;     &lt;lightning:inputField fieldName="Name" /&gt;     &lt;lightning:button class="slds-m-top_small" variant="brand" type="submit" name="update" label="Update" /&gt; &lt;/lightning:recordEditForm&gt;   It looks like below and even the Update-button works as expected and saves the record:    But what I really want is  &lt;lightning:recordEditForm recordId="{!v.recordId}"  objectApiName="{!v.ParentType}"&gt;   Note the {!v.ParentType} instead of the hardcoded "Order"  As an great unfortunate, this only collapses to that error-popup    In the screenshot you see, that the ParentType is correctly bound to v.ParentType (right above the button) which a I also verify in the console  Suspicion  I set the ParentType-attribute as a result of APEX call (asynchronously). This seems to be too late for the lightning:recordEditForm. It tries to render with objectApiName at a time where it is still blank, hence it crashes during initialization...  Question  Is there a clean way to avoid this and get something to fly with an dynamic objectApiName?  Trying to use $A.createComponents()  As @sfdcfox suggested I jump on the $A.createComponents() which turned indeed out to be tricky, exactly as sfdcfox has predicted. Im following this .  Added a button to the markup    Added a handler to the js-controller:  makeEdit : function(cmp, evt, hlp) {      console.log('ParentId :: ', cmp.get("v.ParentId") );      console.log('ParentType :: ', cmp.get("v.ParentType") );      $A.createComponents([             ["lightning:recordEditForm",{                 "recordId"          : cmp.get("v.ParentId"),                 "objectApiName"     : cmp.get("v.ParentType"),             }],             ["lightning:inputField",{                 "fieldName" : "Name"             }]         ]         ,function(components, status, errorMessage){             if (status === "SUCCESS") {                 var wrapper         = components[0];                 var inner           = components[1];                 wrapper.set("v.body", inner);             }             else if (status === "INCOMPLETE") {                 console.log("No response from server or client is offline.")                 // Show offline error             }             else if (status === "ERROR") {                 console.log("Error: " + errorMessage);                 // Show error message             }         }     ); },   Now I can press the button - but nothing - I repeat: n o t h i n g - gets rendered at all. What is my fault here? Note that the console.log get output and the attribute are holding good values.
92960
As can be seen in the Mozilla changlog for JavaScript 1.7 they have added destructuring assignment. Sadly I'm not very fond of the syntax (why write a and b twice?):  var a, b;   [a, b] = f();   Something like this would have been a lot better:  var [a, b] = f();   That would still be backwards compatible. Python-like destructuring would not be backwards compatible.  Anyway the best solution for JavaScript 1.5 that I have been able to come up with is:  function assign(array, map) {     var o = Object();     var i = 0;     $.each(map, function(e, _) {         o[e] = array[i++];     });     return o; }   Which works like:  var array = [1,2]; var _ = assign[array, { var1: null, var2: null }); _.var1; // prints 1 _.var2; // prints 2   But this really sucks because _ has no meaning. It's just an empty shell to store the names. But sadly it's needed because JavaScript doesn't have pointers. On the plus side you can assign default values in the case the values are not matched. Also note that this solution doesn't try to slice the array. So you can't do something like {first: 0, rest: 0}. But that could easily be done, if one wanted that behavior.  What is a better solution?
80088
When referencing class variables, why do people prepend it with this? I'm not talking about the case when this is used to disambiguate from method parameters, but rather when it seems unnecessary.  Example:  public class Person {             private String name;      public String toString() {         return this.name;     } }   In toString, why not just reference name as name?  return name;   What does this.name buy?   a stackoverflow question whose code has this pre-pending.
19901
...where each object also has references to other objects within the same array?  When I first came up with this problem I just though of something like   var clonedNodesArray = nodesArray.clone()   would exist and searched for info on how to clone objects in javascript. I did find a  on StackOverflow (answered by the very same @JohnResig) and he pointed out that with jQuery you could do  var clonedNodesArray = jQuery.extend({}, nodesArray);   to clone an object. I tried this though, this only copies the references of the objects in the array. So if I   nodesArray[0].value = "red" clonedNodesArray[0].value = "green"   the value of both nodesArray[0] and clonedNodesArray[0] will turn out to be "green". Then I tried  var clonedNodesArray = jQuery.extend(true, {}, nodesArray);   which deep copies an Object, but I got "too much recursion" and "control stack overflow" messages from both Firebug and Opera Dragonfly respectively.  How would you do it? Is this something that shouldn't even be done? Is there a reusable way of doing this in Javascript?
22843
I would like to duplicate a mesh object, but on a particular object. Right now this is working:  import bpy   for i in range (0,5):     bpy.ops.object.duplicate(linked=0,mode='TRANSLATION')      bpy.data.objects['cube.001'].animation_data_clear()   The way I want it to do it though is that the script should only duplicate a particular object in the scene that I enter through as a string name. ATM its just duplicating the 1st object and duplicating the duplicates of the duplicates.
6356
I have 2 objects from the same type and i would like to shallow copy one state to the other. In C++ i have memcpy which is great. How can i do it in C#? The MemberwiseClone() is not good enough because it creates &amp; returns a new object and i like to copy to an existing object. I thought of using reflection but i'm afraid it will be too slow for production code. I also thought of using one of the .Net serializers but i think they also create object rather than setting an existing one.  My Use Case:  I have a template object (class not struct) which needs to be updated by one of its instances (objects made of this template)  Any ideas?
123261
Is there any way to redefine a class or some of its methods without using typical inheritance? For example: class third_party_library {     function buggy_function() {         return 'bad result';     }     function other_functions(){         return 'blah';     } }  What can I do to replace buggy_function()? Obviously this is what I would like to do class third_party_library redefines third_party_library{     function buggy_function() {         return 'good result';     }     function other_functions(){         return 'blah';     } }  This is my exact dilemma: I updated a third party library that breaks my code. I don't want to modify the library directly, as future updates could break the code again. I'm looking for a seamless way to replace the class method. I've found this  that says it can do it, but I'm wary as it's 4 years old. EDIT: I should have clarified that I cannot rename the class from third_party_library to magical_third_party_library or anything else because of framework limitations. For my purposes, would it be possible to just add a function to the class? I think you can do this in C# with something called a &quot;partial class.&quot;
110516
I am trying to perform two operations. First I want to increase the size of a GameObject and then delay for 0.1s before returning it back to its original size. However, when I try using StartCoroutine(), it keeps giving me the error :  An object reference is required to access not-static members   even though I made my Delay() function static. Can somebody please tell me what I'm doing wrong?  public static void MoveSelected(int choice) {     GameObject rock1 = GameObject.Find("Rock1");     GameObject paper1 = GameObject.Find("Paper1");     GameObject scissors1 = GameObject.Find("Scissors1");      Vector3 scl;      if (choice == 1)     {         scl = rock1.transform.localScale;         scl.x *= 1.1f;         scl.y *= 1.1f;         rock1.transform.localScale = scl;          StartCoroutine(Delay());          scl.x /= 1.1f;         scl.y /= 1.1f;         rock1.transform.localScale = scl;     }     else if (choice == 2)     {         scl = paper1.transform.localScale;         scl.x *= 1.1f;         scl.y *= 1.1f;         paper1.transform.localScale = scl;          StartCoroutine(Delay());          scl.x /= 1.1f;         scl.y /= 1.1f;         rock1.transform.localScale = scl;     }     else if (choice == 3)     {         scl = scissors1.transform.localScale;         scl.x *= 1.1f;         scl.y *= 1.1f;         scissors1.transform.localScale = scl;          StartCoroutine(Delay());          scl.x /= 1.1f;         scl.y /= 1.1f;         rock1.transform.localScale = scl;     }  }  static IEnumerator Delay() {     yield return new WaitForSeconds(0.1f); }
56234
I am trying to modify an object after importing it. This post is related to   import bpy full_path_to_file = "C:\\Users\\xyz\\Documents\\test.obj" bpy.ops.import_scene.obj(filepath=full_path_to_file) # create the material mat = bpy.data.materials.new('MaterialName') mat.diffuse_color = (1.0,0.0,1.0) mat.diffuse_shader = 'LAMBERT' mat.diffuse_intensity = 1.0  # get the object obj = bpy.data.objects['Cube']  # get the material mat = bpy.data.materials['MaterialName']  # assign material to object obj.data.materials.append(mat)   I wanted to add a material to the imported object but that is not happening. So I tried to scale it even that is not happening.  I realized that no operation is being performed on the imported objects. But the same operations are executing on the objects that are created (i.e. not imported).  Any help would be appreciated.
44630
I want a true deep copy. In Java, this was easy, but how do you do it in C#?
99064
I've searched so long for the answer and all I ever see is the typical "press ctr + j to join objects, switch to Edit mode, and edit as one mesh", or "make duplicates", or "download the MultiEdit addon", etc.  But even as an 'advanced newbie' I already figured out how to edit several objects in Edit mode, but the problem is, they all share the ONE and SAME NAME.  Object mode assigns each object it's own name - but not in Edit mode.  Is this just the way Blender works? Does it not allow you to add objects with their own name (in Edit mode)?  Thanks SO much for this (possibly) over-obvious answer!!  Shelton
42448
You can copy modifiers from one object to other objects with CtrlL> Make links > Modifiers, or the Copy attributes addon.  However, is it possible to apply modifiers to many objects at once?