Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Array.prototype.isArray = true;
- var ObjectCopier = (function () {
- var self = this; //this variable allow to access the public method inside the private one
- function copyArray(arr) { //private function
- var tempArr = [];
- for (var i = 0; i < arr.length; i++) {
- if (typeof (arr[i]) === "object" && arr[i].isArray) {
- tempArr.push(copyArray(arr[i]));
- } else if (typeof (arr[i]) === "object") {
- tempArr.push(self.deepCopy(arr[i]))
- } else {
- tempArr.push(arr[i]);
- }
- }
- return tempArr;
- }
- function deepCopy(object) { //public function
- var tempCopy = {};
- if (typeof (object) === "object" && object.isArray) {
- tempCopy= copyArray(object);
- } else if (typeof(object)==="object") {
- for (prop in object) {
- tempCopy[prop]= this.deepCopy(object[prop]);
- }
- } else {
- tempCopy= object;
- }
- return tempCopy;
- }
- //Make the functions public.
- return {
- copyArray: copyArray,
- deepCopy: deepCopy
- }
- })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement