jsfiddle-console example
https://github.com/eu81273/jsfiddle-console
HTML
<script src="https://cdn.jsdelivr.net/gh/eu81273/jsfiddle-console/console.js"></script>
JavaScript
//https://stackoverflow.com/questions/728360/how-do-i-correctly-clone-a-javascript-object
function deepClone(obj)
{
var copy;
// Handle the 3 simple types, and null or undefined
if (null == obj || "object" != typeof obj) return obj;
// Handle Array
if (obj instanceof Array) {
copy = [];
for (var i = 0, len = obj.length; i < len; i++) {
copy[i] = deepClone(obj[i]);
}
return copy;
}
// Handle Object
if (obj instanceof Object) {
copy = {};
for (var attr in obj) {
if (obj.hasOwnProperty(attr)) copy[attr] = deepClone(obj[attr]);
}
return copy;
}
throw new Error("Unable to copy obj! Its type isn't supported.");
}
function A()
{
this.param1 = {'a': 1};
this.param2 = 'b';
}
A.prototype.getParam1 = function()
{
return this.param1;
}
function B(objectToCopyFrom)
{
//Code to copy all properties from the objectToCopyFrom and inherite prototype.
Object.assign(this, deepClone(objectToCopyFrom));
this.param1.a += 2;
this.param3 = 'c';
}
B.prototype = Object.create(A.prototype);
B.prototype.getParam3 = function()
{
return this.param3;
}
var a = new A();
a.param1 = {'a': 2};
var b = new B(a);
console.log(b.param3); //Should print: "c"
console.log(JSON.stringify( b.getParam1() )); //Should print: "{'a': 4}"
a.param1.a = 8;
console.log(JSON.stringify( b.getParam1() )); //Still should: "{'a': 4}"