Created
August 29, 2017 14:14
-
-
Save nuwansh/bf3b83f421a21bf0c4b867b9a93e0b8f to your computer and use it in GitHub Desktop.
JS Bin // source https://jsbin.com/kiwutap
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<meta name="viewport" content="width=device-width"> | |
<title>JS Bin</title> | |
</head> | |
<body> | |
<script id="jsbin-javascript"> | |
/* | |
const book = { | |
"book": { | |
name: "Gmperaliya", | |
author: "Martin", | |
chapters: [ | |
{name: 'Chpter 01', pages: "1-10"}, | |
{name: 'Chpter 2', pages: "20-30"} | |
]}, | |
"company": { subdomain: "test", color: "red" } | |
} | |
*/ | |
const book = { | |
'chapters': [ | |
{name: 'Chpter 01', pages: "1-10"}, | |
{name: 'Chpter 2', pages: "20-30"} | |
] | |
} | |
function decorate(obj, decoration) { | |
var objNew = Object.create(obj); | |
for(var i in decoration) { | |
if (Object.prototype.toString.call(objNew[i]) === '[object Object]') { | |
for(var j in decoration[i]) { | |
if (Array.isArray(objNew[i][j]) ) { | |
for(var f in decoration[i][j]) { | |
objNew[i][j].forEach(function(arrayItem, index) { | |
objNew[i][j][index][f] = decoration[i][j][f]; | |
}); | |
} | |
} else { | |
objNew[i][j] = decoration[i][j]; | |
} | |
} | |
} else if (Object.prototype.toString.call(objNew[i]) === '[object Array]') { | |
for(var f in decoration[i]) { | |
objNew[i].forEach(function(arrayItem, index) { | |
objNew[i][index][f] = decoration[i][f]; | |
}); | |
} | |
} | |
} | |
return objNew; | |
} | |
/* | |
const output = decorate(book, { | |
book: { | |
getName: function() { return this.name; }, | |
chapters: { | |
getName: function() { return `${this.name}-${this.pages}`; }, | |
} | |
}, | |
company: { | |
getCompanyName: function() { return this.subdomain; } | |
} | |
}); | |
*/ | |
const output = decorate(book, { | |
chapters: { | |
getName: function() { return `${this.name}-${this.pages}`; } | |
} | |
}); | |
console.log(output.chapters); | |
/* console.log(output.book.chapters[1].getName()); */ | |
/* | |
console.log(output.company.getCompanyName()); | |
*/ | |
</script> | |
<script id="jsbin-source-javascript" type="text/javascript">/* | |
const book = { | |
"book": { | |
name: "Gmperaliya", | |
author: "Martin", | |
chapters: [ | |
{name: 'Chpter 01', pages: "1-10"}, | |
{name: 'Chpter 2', pages: "20-30"} | |
]}, | |
"company": { subdomain: "test", color: "red" } | |
} | |
*/ | |
const book = { | |
'chapters': [ | |
{name: 'Chpter 01', pages: "1-10"}, | |
{name: 'Chpter 2', pages: "20-30"} | |
] | |
} | |
function decorate(obj, decoration) { | |
var objNew = Object.create(obj); | |
for(var i in decoration) { | |
if (Object.prototype.toString.call(objNew[i]) === '[object Object]') { | |
for(var j in decoration[i]) { | |
if (Array.isArray(objNew[i][j]) ) { | |
for(var f in decoration[i][j]) { | |
objNew[i][j].forEach(function(arrayItem, index) { | |
objNew[i][j][index][f] = decoration[i][j][f]; | |
}); | |
} | |
} else { | |
objNew[i][j] = decoration[i][j]; | |
} | |
} | |
} else if (Object.prototype.toString.call(objNew[i]) === '[object Array]') { | |
for(var f in decoration[i]) { | |
objNew[i].forEach(function(arrayItem, index) { | |
objNew[i][index][f] = decoration[i][f]; | |
}); | |
} | |
} | |
} | |
return objNew; | |
} | |
/* | |
const output = decorate(book, { | |
book: { | |
getName: function() { return this.name; }, | |
chapters: { | |
getName: function() { return `${this.name}-${this.pages}`; }, | |
} | |
}, | |
company: { | |
getCompanyName: function() { return this.subdomain; } | |
} | |
}); | |
*/ | |
const output = decorate(book, { | |
chapters: { | |
getName: function() { return `${this.name}-${this.pages}`; } | |
} | |
}); | |
console.log(output.chapters); | |
/* console.log(output.book.chapters[1].getName()); */ | |
/* | |
console.log(output.company.getCompanyName()); | |
*/</script></body> | |
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
const book = { | |
"book": { | |
name: "Gmperaliya", | |
author: "Martin", | |
chapters: [ | |
{name: 'Chpter 01', pages: "1-10"}, | |
{name: 'Chpter 2', pages: "20-30"} | |
]}, | |
"company": { subdomain: "test", color: "red" } | |
} | |
*/ | |
const book = { | |
'chapters': [ | |
{name: 'Chpter 01', pages: "1-10"}, | |
{name: 'Chpter 2', pages: "20-30"} | |
] | |
} | |
function decorate(obj, decoration) { | |
var objNew = Object.create(obj); | |
for(var i in decoration) { | |
if (Object.prototype.toString.call(objNew[i]) === '[object Object]') { | |
for(var j in decoration[i]) { | |
if (Array.isArray(objNew[i][j]) ) { | |
for(var f in decoration[i][j]) { | |
objNew[i][j].forEach(function(arrayItem, index) { | |
objNew[i][j][index][f] = decoration[i][j][f]; | |
}); | |
} | |
} else { | |
objNew[i][j] = decoration[i][j]; | |
} | |
} | |
} else if (Object.prototype.toString.call(objNew[i]) === '[object Array]') { | |
for(var f in decoration[i]) { | |
objNew[i].forEach(function(arrayItem, index) { | |
objNew[i][index][f] = decoration[i][f]; | |
}); | |
} | |
} | |
} | |
return objNew; | |
} | |
/* | |
const output = decorate(book, { | |
book: { | |
getName: function() { return this.name; }, | |
chapters: { | |
getName: function() { return `${this.name}-${this.pages}`; }, | |
} | |
}, | |
company: { | |
getCompanyName: function() { return this.subdomain; } | |
} | |
}); | |
*/ | |
const output = decorate(book, { | |
chapters: { | |
getName: function() { return `${this.name}-${this.pages}`; } | |
} | |
}); | |
console.log(output.chapters); | |
/* console.log(output.book.chapters[1].getName()); */ | |
/* | |
console.log(output.company.getCompanyName()); | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment