Something Like that:
Reflection and Extend
What is Reflection in javascript
An Object can look at itself, listing and changing its properties and methods.
JS
var person = { firstname: 'Default', lastname: 'Default', getFullName: function(){ return this.firstname + ' ' + this.lastname; } } var john = { firstname: 'John', lastname: 'Doe' } john.__proto__ = person; for(var prop in john){ if(john.hasOwnProperty(prop)){ document.write(prop + ': ' + john[prop] + '
'); } }
[codepen_embed height=”265″ theme_id=”0″ slug_hash=”PKRgaO” default_tab=”result” user=”pradeepanvi”]See the Pen Reflection in javascript by Pradeep Kumar (@pradeepanvi) on CodePen.[/codepen_embed]
Now you can see
firstname: John
lastname: Doe
Extend (use underscore.js)
JS
var john = { firstname: 'John', lastname: 'Doe' } var jane = { address: '111 Main St.', getFormalFullName: function(){ return this.lastname + ', ' + this.firstname; } } var jim = { getFirstName: function(){ return firstname; } } _.extend(john, jane, jim); document.write(john);
[codepen_embed height=”265″ theme_id=”0″ slug_hash=”qXowLN” default_tab=”result” user=”pradeepanvi”]See the Pen Extend in javascript by Pradeep Kumar (@pradeepanvi) on CodePen.[/codepen_embed]
Now you can see
[object Object]