Skip to content

Instantly share code, notes, and snippets.

@hoffination
Last active June 28, 2017 18:06
Show Gist options
  • Save hoffination/35427c0668dfbb6648610d45b5057c3a to your computer and use it in GitHub Desktop.
Save hoffination/35427c0668dfbb6648610d45b5057c3a to your computer and use it in GitHub Desktop.
JavaScript Object Shadowing
(function() {
'use strict';
// Define a generic prototype for our skiiers to inherit
const skiierPrototype = {
name: 'fred',
greet: function() {
return 'Hi, I am ' + this.name
}
}
let ted = Object.create(skiierPrototype)
console.log(ted.greet()); // Hi, I am fred
ted.name = 'Ted Ligety';
console.log(ted.greet()); // Hi, I am Ted Ligety
delete ted.name;
console.log(ted.greet()); // Hi, I am fred
console.log('--------------------------------')
function Skiier() {
this.name = 'fred';
this.greet = () => {
return 'Hi, I am ' + this.name
}
}
// https://en.wikipedia.org/wiki/Julia_Mancuso#Olympic_results
let julia = new Skiier()
console.log(julia.greet()); // Hi, I am fred
julia.name = 'Julia Mancuso';
console.log(julia.greet()); // Hi, I am Julia Mancuso
delete julia.name;
console.log(julia.greet()); // Hi, I am undefined
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment