Created
December 9, 2013 04:12
-
-
Save jonathanmarvens/7867291 to your computer and use it in GitHub Desktop.
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
(function () { | |
function Animal() {} | |
Object.defineProperties(Animal.prototype, { | |
noise: { | |
configurable: false, | |
enumerable: false, | |
value: null, | |
writable: false | |
} | |
}); | |
Animal.prototype.makeNoise = function () { | |
throw new Error('You must override this method.'); | |
}; | |
function Dog() { | |
Animal.call(this); | |
} | |
Dog.prototype = Object.create(Animal.prototype); | |
Object.defineProperties(Dog.prototype, { | |
noise: { | |
configurable: false, | |
enumerable: false, | |
value: 'woof', | |
writable: false | |
} | |
}); | |
Dog.prototype.constructor = Dog; | |
Dog.prototype.makeNoise = function () { | |
console.log(this.noise + '!'); | |
}; | |
var snoopy = new Dog(); | |
if (snoopy instanceof Animal) { | |
console.log('snoopy is an instance of Animal.'); | |
} | |
if (snoopy instanceof Dog) { | |
console.log('snoopy is an instance of Dog.'); | |
} | |
snoopy.makeNoise(); | |
}).call(this); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment