Skip to content

Instantly share code, notes, and snippets.

@etoxin
Last active May 14, 2018 21:00
Show Gist options
  • Save etoxin/afcd1c7938e24f07d92833f08bf02e5c to your computer and use it in GitHub Desktop.
Save etoxin/afcd1c7938e24f07d92833f08bf02e5c to your computer and use it in GitHub Desktop.
Sharing between Objects
var Vehicle = {
describe: function () {
return `This ${this.type} is the ${this.make} ${this.model}.`;
}
}
var Car = Object.create(Vehicle, {
type: {value: 'Car', writable: true}
});
var Holden = Object.create(Car, {
make: {value: 'Holden', writable: true}
});
var Commodore = Object.create(Holden, {
model: {value: 'Commodore', writable: true}
});
console.log(Commodore.describe());
// "This Car is the Holden Commodore."
var Vehicle = {
describe: function () {
return `This ${this.type} is the ${this.make} ${this.model}.`;
}
}
var Car = {
__proto__: Vehicle,
type: 'Car',
}
var Holden = {
__proto__: Car,
make: 'Holden',
}
var Commodore = {
__proto__: Holden,
model: 'Commodore',
}
console.log(Commodore.describe());
// "This Car is the Holden Commodore."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment