clone

Javascript - How to clone an object?

百般思念 提交于 2019-12-03 22:31:43
I am confused. I create a copy from myObjOne , than i delete an entry from myObjOne and JS delete the entry in my copy( myObjTwo ) too? But why? myObjOne = {}; myObjOne['name'] = 'xxx'; myObjOne['id'] = 'yyy'; myObjOne['plz'] = 'zzz'; // clone myObjTwo = myObjOne; // remove something delete myObjOne['name']; console.dir(myObjTwo); example http://jsbin.com/itixes/edit#javascript,html Narendra Yadala Update: Removing Object.create as a method of cloning as indicated in comments. myObjTwo = myObjOne; does not clone. It simply copies the reference. If you want to clone, you can use JSON.parse and

How can I clone a DateTime object in C#?

牧云@^-^@ 提交于 2019-12-03 22:01:19
How can I clone a DateTime object in C#? Pieter van Ginkel DateTime is a value type ( struct ) This means that the following creates a copy: DateTime toBeClonedDateTime = DateTime.Now; DateTime cloned = toBeClonedDateTime; You can also safely do things like: var dateReference = new DateTime(2018, 7, 29); for (var h = 0; h < 24; h++) { for (var m = 0; m < 60; m++) { var myDateTime = dateReference.AddHours(h).AddMinutes(m); Console.WriteLine("Now at " + myDateTime.ToShortDateString() + " " + myDateTime.ToShortTimeString()); } } Note how in the last example myDateTime gets declared anew in each

How to clone or freeze an Android database cursor

佐手、 提交于 2019-12-03 21:57:06
问题 I have a custom cursor adapter, and I would like to pass each 'row' of the cursor back to the application (via a registered callback which is working). I know I could read each of the fields from the cursor and do it manually, but I would simply like to pass a 'frozen clone' of the cursor back. (Reading the fields in the adapter would require me to make several specialised versions of this class.) Ideally I would like to pass back something with the same interface as Cursor, but which couldn

Java中的克隆Cloneable

杀马特。学长 韩版系。学妹 提交于 2019-12-03 21:49:40
浅克隆与深克隆 当拷贝一个变量时,原始变量与拷贝变量引用了同一个对象。那么当改变一个变量所引用的对象时,就会对另一个变量产生影响。形象化一点说,就像某人A有一把遥控,用来控制电视,他把遥控交给了B,B用遥控来改变电视台,则对 A来说,他看的节目台也发生相应变化。 Employee e1 =new Employee(“John”,5000); Employee e2=e1; e2.raiseSalary(500); 那么,如果我们希望得到一个拷贝,它与原拷贝互不影响,则就引入了克隆的概念。 Employee e2=e1.clone(); 这会产生一个新的Employee对象e2,它与被克隆对象e1有相同的属性。并且e2与e1互不影响。 浅克隆 clone()方法是object类的proteced方法,由于这个类对于具体类一无所知,所以只能对各个域进行拷贝。 如果原始对象与拷贝对象的共享的子对象是不可变的,将不会产生任何问题。 例如, ①对象中的所有数据域是数值或基本类型 ② 像String这样不允许改变的类 ③ 在其生命周期中不会发生变化,既没有修改它们的方法,也没有创建对 它们的引用 的方法 浅克隆 域中 包含对象的引用 ,则会使得克隆后的对象和以前的对象有着相同引用的域。这种只是单纯进行域的拷贝就称作 浅克隆 ,而默认的方法就是浅克隆。 深克隆 但是

Is 'pull' a synonym for 'clone' in a Mercurial source-control repository?

假如想象 提交于 2019-12-03 20:09:43
问题 I'm seeing the command 'pull' and wondering how that's different from a 'clone'. Both terms seem to imply retrieving code from some remote repository. Is there some subtle distinction here? 回答1: hg clone is how you make a local copy of a remote repository. The Subversion equivalent is svn checkout . hg pull pulls changes from another repository. hg update applies those changes to the local repository. hg pull -u is equivalent to hg pull; hg update . The Subversion equivalent to hg pull -u is

Can clone method create object using constructor

こ雲淡風輕ζ 提交于 2019-12-03 20:03:27
问题 I always thought that clone() creates an object without calling a constructor. But, while reading Effective Java Item 11: Override clone judiciously , I found a statement which says that The provision that “no constructors are called” is too strong. A well-behaved clone method can call constructors to create objects internal to the clone under construction. If the class is final, clone can even return an object created by a constructor. Can someone please explain this to me? 回答1: I always

Duplicating a Ruby array of strings

荒凉一梦 提交于 2019-12-03 19:56:12
问题 arr = ["red","green","yellow"] arr2 = arr.clone arr2[0].replace("blue") puts arr.inspect puts arr2.inspect produces: ["blue", "green", "yellow"] ["blue", "green", "yellow"] Is there anyway to do a deep copy of an array of strings, other than using Marshal as i understand that is a hack. I could do: arr2 = [] arr.each do |e| arr2 << e.clone end but it doesn't seem very elegant, or efficient. Thanks 回答1: Your second solution can be shortened to arr2 = arr.map do |e| e.dup end (unless you

How do I clone a large Git repository on an unreliable connection?

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-03 19:45:55
问题 I want to clone LibreOffice. From the official website, this is what's written: All our source code is hosted in git: Clone: $ git clone git://anongit.freedesktop.org/libreoffice/core # (browse) Clone (http): $ git clone http://anongit.freedesktop.org/git/libreoffice/core.git # slower Tarballs: http://download.documentfoundation.org/libreoffice/src/ please find the latest versions (usually near the bottom) now, when I write this command in git bash to clone, it starts fetching. But the

getComputedStyle of a clone element which is not in the dom

女生的网名这么多〃 提交于 2019-12-03 18:54:53
I have a html page like; <div id="cloneDiv"> <h1> <a href="">ASUS Vivotab Smart Black Office 2013 H&S ME400C-C2-BK 10.1-Inch 64GB Tablet (Black)</a> </h1> <div id="fordBackgroundImage" style="display: inline-block; background-position: center center; width: 1200px; height: 565px; background-image: url('http://www.ford.com/ngbs-services/resources/ford/edge/2013/npcolorizer/edg13_ingotsilver.jpg'); background-repeat: no-repeat; background-attachment: scroll; position: relative;"> <div style="position: absolute; color: white; font-weight: bold; top: 50px; left: 100px; font-size: 50px;"> Try new

When (deep) Cloning, use String.Copy or str1 = str2?

倖福魔咒の 提交于 2019-12-03 17:21:16
When (deep) Cloning a custom object, should I use clone.str1 = String.Copy(obj.str1) or clone.str1 = obj.str1 ? I'd prefer the latter - shorter and quicker, but is it "safe"? I'd point to this thread , but, however, not sure what to use. Yes - the latter choice (simple assignment) is safe for strings (in managed code), as this code illustrates: string s1 = "Initial Value"; string s2 = s1; Console.WriteLine("String1: " + s1); Console.WriteLine("String2: " + s2); s1 = "New Value"; Console.WriteLine("String1 - after change: " + s1); Console.WriteLine("String2 - after change: " + s2); Output: