Guest User

Untitled

a guest
Jun 23rd, 2018
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.14 KB | None | 0 0
  1. function clone(obj) {
  2. if (null == obj || "object" != typeof obj) return obj;
  3. var copy = obj.constructor();
  4. for (var attr in obj) {
  5. if (obj.hasOwnProperty(attr)) copy[attr] = obj[attr];
  6. }
  7. return copy;
  8. }
  9.  
  10. var d1 = new Date();
  11.  
  12. /* Wait for 5 seconds. */
  13. var start = (new Date()).getTime();
  14. while (new Date()).getTime() - start < 5000);
  15.  
  16.  
  17. var d2 = clone(d1);
  18. alert("d1 = " + d1.toString() + "nd2 = " + d2.toString());
  19.  
  20. function clone(obj) {
  21. // Handle the 3 simple types, and null or undefined
  22. if (null == obj || "object" != typeof obj) return obj;
  23.  
  24. // Handle Date
  25. if (obj instanceof Date) {
  26. var copy = new Date();
  27. copy.setTime(obj.getTime());
  28. return copy;
  29. }
  30.  
  31. // Handle Array
  32. if (obj instanceof Array) {
  33. var copy = [];
  34. for (var i = 0, var len = obj.length; i < len; ++i) {
  35. copy[i] = clone(obj[i]);
  36. }
  37. return copy;
  38. }
  39.  
  40. // Handle Object
  41. if (obj instanceof Object) {
  42. var copy = {};
  43. for (var attr in obj) {
  44. if (obj.hasOwnProperty(attr)) copy[attr] = clone(obj[attr]);
  45. }
  46. return copy;
  47. }
  48.  
  49. throw new Error("Unable to copy obj! Its type isn't supported.");
  50. }
  51.  
  52. // This would be cloneable:
  53. var tree = {
  54. "left" : { "left" : null, "right" : null, "data" : 3 },
  55. "right" : null,
  56. "data" : 8
  57. };
  58.  
  59. // This would kind-of work, but you would get 2 copies of the
  60. // inner node instead of 2 references to the same copy
  61. var directedAcylicGraph = {
  62. "left" : { "left" : null, "right" : null, "data" : 3 },
  63. "data" : 8
  64. };
  65. directedAcyclicGraph["right"] = directedAcyclicGraph["left"];
  66.  
  67. // Cloning this would cause a stack overflow due to infinite recursion:
  68. var cylicGraph = {
  69. "left" : { "left" : null, "right" : null, "data" : 3 },
  70. "data" : 8
  71. };
  72. cylicGraph["right"] = cylicGraph;
  73.  
  74. function clone(obj){
  75.  
  76. if(obj == null || typeof(obj) != 'object')
  77.  
  78. return obj;
  79.  
  80.  
  81.  
  82. var temp = new obj.constructor();
  83.  
  84. for(var key in obj)
  85.  
  86. temp[key] = clone(obj[key]);
  87.  
  88.  
  89.  
  90. return temp;
  91.  
  92. }
  93.  
  94. Object.prototype.clone = function() {
  95. var newObj = (this instanceof Array) ? [] : {};
  96. for (i in this) {
  97. if (i == 'clone') continue;
  98. if (this[i] && typeof this[i] == "object") {
  99. newObj[i] = this[i].clone();
  100. } else newObj[i] = this[i]
  101. } return newObj;
  102. };
  103.  
  104. var copiedObject = jQuery.extend({},originalObject);
  105.  
  106. var y = Object.clone(x);
  107.  
  108. ...
  109. Object.extend = function(destination, source) {
  110. for (var property in source)
  111. destination[property] = source[property];
  112. return destination;
  113. };
  114. ...
  115. clone: function(object) {
  116. return Object.extend({ }, object);
  117. }
  118. ...
  119.  
  120. // Create an inner object with a variable x whose default
  121. // value is 3.
  122. function innerObj()
  123. {
  124. this.x = 3;
  125. }
  126. innerObj.prototype.clone = function() {
  127. var temp = new innerObj();
  128. for (myvar in this) {
  129. // this object does not contain any objects, so
  130. // use the lightweight copy code.
  131. temp[myvar] = this[myvar];
  132. }
  133. return temp;
  134. }
  135.  
  136. // Create an outer object with a variable y whose default
  137. // value is 77.
  138. function outerObj()
  139. {
  140. // The outer object contains an inner object. Allocate it here.
  141. this.inner = new innerObj();
  142. this.y = 77;
  143. }
  144. outerObj.prototype.clone = function() {
  145. var temp = new outerObj();
  146. for (myvar in this) {
  147. if (this[myvar].clone) {
  148. // This variable contains an object with a
  149. // clone operator. Call it to create a copy.
  150. temp[myvar] = this[myvar].clone();
  151. } else {
  152. // This variable contains a scalar value,
  153. // a string value, or an object with no
  154. // clone function. Assign it directly.
  155. temp[myvar] = this[myvar];
  156. }
  157. }
  158. return temp;
  159. }
  160.  
  161. // Allocate an outer object and assign non-default values to variables in
  162. // both the outer and inner objects.
  163. outer = new outerObj;
  164. outer.inner.x = 4;
  165. outer.y = 16;
  166.  
  167. // Clone the outer object (which, in turn, clones the inner object).
  168. newouter = outer.clone();
  169.  
  170. // Verify that both values were copied.
  171. alert('inner x is '+newouter.inner.x); // prints 4
  172. alert('y is '+newouter.y); // prints 16
Add Comment
Please, Sign In to add comment