Created
May 20, 2016 04:58
-
-
Save slugbyte/dd8138b41ada88d38025a29c8a66c3a9 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
| 'use strict'; | |
| // define a note constructor | |
| function Note(name, content){ | |
| this.name = name; | |
| this.content = content; | |
| } | |
| // add a print method to the note constructor's prototype | |
| Note.prototype.print = function(){ | |
| console.log(this.content); | |
| }; | |
| function Reminder(name, content, time){ | |
| // inherrit Note's properties | |
| Note.call(this, name, content); | |
| this.time = time; | |
| } | |
| // inherrit Note's methods, but still have unique prototype | |
| Reminder.prototype = Object.create(Note.prototype); | |
| // instantiant note a | |
| var a = new Note('a', 'test a'); | |
| // instantiant reminder b | |
| var b = new Reminder('b', 'test b', new Date()); | |
| // invoke the print method on notes a and b | |
| a.print(); // test a | |
| b.print(); // test b | |
| // change Note's prototype's print method | |
| Note.prototype.print = function(){ | |
| console.log('lul'); | |
| }; | |
| // changing Notes's prototype's print method also changed the print methods on a's and b's prototype | |
| a.print(); // lul | |
| b.print(); // lul | |
| // change b's proto's proto's print method | |
| b.__proto__.__proto__.print = function(){ | |
| console.log('wat'); | |
| }; | |
| a.print(); // wat | |
| b.print(); // wat | |
| // change a's proto's print method | |
| a.__proto__.print = function(){ | |
| console.log('duuuh'); | |
| }; | |
| // changing a's proto's print changes a's and b's print method | |
| a.print(); // duuuh | |
| b.print(); // duuuh | |
| // change Reminders protoytpe's print method | |
| Reminder.prototype.print = function(){ | |
| console.log(this.content + '!!!!!!'); | |
| }; | |
| // changing Reminders prototype only changes b's | |
| a.print(); // wat | |
| b.print(); // test b!!!!!! | |
| b.__proto__.print = function(){ | |
| console.log("wat wat"); | |
| }; | |
| // changing b's proto's print only changes b's | |
| a.print(); // duuuh | |
| b.print(); // wat wat |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment