Last active
December 18, 2015 00:38
-
-
Save jasonblanchard/fa3873c15c0747229bec 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 a Person constructor function that must be called with the `new` keyword. | |
| // Prototype link between instances and Person.prototype is implicitly created by calling the Person function with the `new` keyword. | |
| function Person(firstName, lastName) { | |
| this.firstName = firstName; | |
| this.lastName = lastName; | |
| this.isActive = true; | |
| } | |
| Person.prototype.sayName = function() { | |
| console.log("My name is " + this.firstName); | |
| }; | |
| var person1 = new Person('gob', 'bluth'); | |
| person1.sayName(); | |
| var person2 = new Person('tobias', 'funke'); | |
| person2.sayName(); |
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
| // Create a Person prototype object and explicitly make a prototype link to instances with Object.create() | |
| // You must write your own function to initialize instances with default data (here, we call it `build`, but it can be called anything). | |
| var Person = { | |
| build: function(firstName, lastName) { | |
| this.firstName = firstName; | |
| this.lastName = lastName; | |
| this.isActive = true; | |
| return this; | |
| }, | |
| sayName: function() { | |
| console.log("My name is " + this.firstName); | |
| } | |
| }; | |
| var person1 = Object.create(Person); | |
| person1.build("gob", "bluth"); | |
| person1.sayName(); | |
| var person2 = Object.create(Person); | |
| person2.build("lucille", "bluth"); | |
| person2.sayName(); | |
| // Make your life easier with a factory function. | |
| function personFactory(firstName, lastName) { | |
| var tmpPerson = Object.create(Person); | |
| tmpPerson.build(firstName, lastName); | |
| return tmpPerson; | |
| } | |
| var person3 = personFactory('tobias', 'funke'); | |
| person3.sayName(); |
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 the new ES6 `class` keyword. Results in basically the same code as what's in person_v1.js. | |
| // NOTE: This will not work in most browsers. You must "transpile" the code to es6 using Babel https://babeljs.io/ | |
| class Person { | |
| constructor(firstName, lastName) { | |
| this.firstName = firstName; | |
| this.lastName = lastName; | |
| this.isActive = true; | |
| } | |
| sayName() { | |
| console.log("My name is " + this.firstName); | |
| } | |
| } | |
| var person1 = new Person('gob', 'bluth'); | |
| person1.sayName(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment