{"size": 1581, "ext": "js", "max_stars_repo_path": "JavaScript/JS_Part_One/Homeworks/Js_part_One_Homeworks/10_Using-Objects/scripts/3_DeepCopy.js", "max_stars_repo_name": "razsilev/TelerikAcademy_Homework", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "JavaScript/JS_Part_One/Homeworks/Js_part_One_Homeworks/10_Using-Objects/scripts/3_DeepCopy.js", "max_issues_repo_name": "razsilev/TelerikAcademy_Homework", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "JavaScript/JS_Part_One/Homeworks/Js_part_One_Homeworks/10_Using-Objects/scripts/3_DeepCopy.js", "max_forks_repo_name": "razsilev/TelerikAcademy_Homework", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 25.0952380952, "max_line_length": 73, "alphanum_fraction": 0.6065781151}
﻿/*
 *   3.   Write a function that makes a deep copy of an object
 *        The function should work for both primitive and reference types
 */
function clone(obj) {
    if (obj === null || typeof (obj) !== 'object') {
        return obj;
    }

    var copy = {},
        key;
    for (key in obj) {
        // recursiv copy propertis, because they olso can be objects
        copy[key] = clone(obj[key]);
    }

    return copy;
}

function car(owner, brand, model) {
    return {
        owner: owner,
        brand: brand,
        model: model,
        toString: function () {
            return this.owner + ': ' + this.brand + ' - ' + this.model;
        }
    };
}

function person(firstName, lastName) {
    return {
        firstName: firstName,
        lastName: lastName,
        toString: function () {
            return this.firstName + ' ' + this.lastName;
        }
    };
}

var number = 5;
var numberCopy = clone(number);
var owner = person('Petar', 'Petrov');
var car = car(owner, 'lada', 'samara');
var carCopy = clone(car);

jsConsole.writeLine('number: ' + number);
jsConsole.writeLine('numberCopy: ' + numberCopy);
jsConsole.writeLine('car: ' + car);
jsConsole.writeLine('carCopy: ' + carCopy);
jsConsole.writeLine('');
jsConsole.writeLine('change original objects');
jsConsole.writeLine('');

// change original objects
number = 6;
car.model = 'niva';
car.owner.firstName = 'Georgy';

jsConsole.writeLine('number: ' + number);
jsConsole.writeLine('numberCopy: ' + numberCopy);
jsConsole.writeLine('car: ' + car);
jsConsole.writeLine('carCopy: ' + carCopy);