Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function clone(obj) {
- if (null == obj || "object" != typeof obj) return obj;
- var copy = obj.constructor();
- for (var attr in obj) {
- if (obj.hasOwnProperty(attr)) copy[attr] = obj[attr];
- }
- return copy;
- }
- var d1 = new Date();
- /* Wait for 5 seconds. */
- var start = (new Date()).getTime();
- while (new Date()).getTime() - start < 5000);
- var d2 = clone(d1);
- alert("d1 = " + d1.toString() + "nd2 = " + d2.toString());
- function clone(obj) {
- // Handle the 3 simple types, and null or undefined
- if (null == obj || "object" != typeof obj) return obj;
- // Handle Date
- if (obj instanceof Date) {
- var copy = new Date();
- copy.setTime(obj.getTime());
- return copy;
- }
- // Handle Array
- if (obj instanceof Array) {
- var copy = [];
- for (var i = 0, var len = obj.length; i < len; ++i) {
- copy[i] = clone(obj[i]);
- }
- return copy;
- }
- // Handle Object
- if (obj instanceof Object) {
- var copy = {};
- for (var attr in obj) {
- if (obj.hasOwnProperty(attr)) copy[attr] = clone(obj[attr]);
- }
- return copy;
- }
- throw new Error("Unable to copy obj! Its type isn't supported.");
- }
- // This would be cloneable:
- var tree = {
- "left" : { "left" : null, "right" : null, "data" : 3 },
- "right" : null,
- "data" : 8
- };
- // This would kind-of work, but you would get 2 copies of the
- // inner node instead of 2 references to the same copy
- var directedAcylicGraph = {
- "left" : { "left" : null, "right" : null, "data" : 3 },
- "data" : 8
- };
- directedAcyclicGraph["right"] = directedAcyclicGraph["left"];
- // Cloning this would cause a stack overflow due to infinite recursion:
- var cylicGraph = {
- "left" : { "left" : null, "right" : null, "data" : 3 },
- "data" : 8
- };
- cylicGraph["right"] = cylicGraph;
- function clone(obj){
- if(obj == null || typeof(obj) != 'object')
- return obj;
- var temp = new obj.constructor();
- for(var key in obj)
- temp[key] = clone(obj[key]);
- return temp;
- }
- Object.prototype.clone = function() {
- var newObj = (this instanceof Array) ? [] : {};
- for (i in this) {
- if (i == 'clone') continue;
- if (this[i] && typeof this[i] == "object") {
- newObj[i] = this[i].clone();
- } else newObj[i] = this[i]
- } return newObj;
- };
- var copiedObject = jQuery.extend({},originalObject);
- var y = Object.clone(x);
- ...
- Object.extend = function(destination, source) {
- for (var property in source)
- destination[property] = source[property];
- return destination;
- };
- ...
- clone: function(object) {
- return Object.extend({ }, object);
- }
- ...
- // Create an inner object with a variable x whose default
- // value is 3.
- function innerObj()
- {
- this.x = 3;
- }
- innerObj.prototype.clone = function() {
- var temp = new innerObj();
- for (myvar in this) {
- // this object does not contain any objects, so
- // use the lightweight copy code.
- temp[myvar] = this[myvar];
- }
- return temp;
- }
- // Create an outer object with a variable y whose default
- // value is 77.
- function outerObj()
- {
- // The outer object contains an inner object. Allocate it here.
- this.inner = new innerObj();
- this.y = 77;
- }
- outerObj.prototype.clone = function() {
- var temp = new outerObj();
- for (myvar in this) {
- if (this[myvar].clone) {
- // This variable contains an object with a
- // clone operator. Call it to create a copy.
- temp[myvar] = this[myvar].clone();
- } else {
- // This variable contains a scalar value,
- // a string value, or an object with no
- // clone function. Assign it directly.
- temp[myvar] = this[myvar];
- }
- }
- return temp;
- }
- // Allocate an outer object and assign non-default values to variables in
- // both the outer and inner objects.
- outer = new outerObj;
- outer.inner.x = 4;
- outer.y = 16;
- // Clone the outer object (which, in turn, clones the inner object).
- newouter = outer.clone();
- // Verify that both values were copied.
- alert('inner x is '+newouter.inner.x); // prints 4
- alert('y is '+newouter.y); // prints 16
Add Comment
Please, Sign In to add comment