Created
July 26, 2014 14:18
-
-
Save vc0d35/7c81d4f2a90d038fa711 to your computer and use it in GitHub Desktop.
Prototypal Javascript
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 Animal(name){ | |
| this.name = name; | |
| } | |
| Animal.prototype.getName = function(){ | |
| return this.name; | |
| } | |
| function Dog(name){ | |
| Animal.call(this, name); | |
| } | |
| Dog.prototype = Object.create(Animal.prototype); //Clonando o objeto | |
| Dog.prototype.speak = function(){ | |
| return "Woof! Woof!"; | |
| } | |
| var myDog = new Dog("Totó"); | |
| console.log("My dog "+myDog.getName()+" has learned a new trick: "+myDog.speak()); |
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
| class Animal { | |
| constructor(name) { | |
| this.name = name; | |
| } | |
| getName() { | |
| return this.name; | |
| } | |
| } | |
| class Dog extends Animal { | |
| constructor(name) { | |
| super(name); | |
| } | |
| speak() { | |
| return "woof"; | |
| } | |
| } | |
| var dog = new Dog("Scamp"); | |
| console.log(dog.getName() + ' says ' + dog.speak()); |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Credits: http://jurberg.github.io/blog/2014/07/12/javascript-prototype/