Created
July 24, 2013 02:32
-
-
Save ryanburnette/6067677 to your computer and use it in GitHub Desktop.
JavaScript prototype pattern.
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 () { | |
| 'use strict'; | |
| var proto | |
| , bob | |
| , alice | |
| , bobby | |
| ; | |
| function Person(name) { | |
| if ( !(this instanceof Person) ) { | |
| return Person.create(name); | |
| } | |
| this.name = name || "(name tag fell off)"; | |
| } | |
| proto = Person.prototype; | |
| proto.greet = function (other) { | |
| var me = this | |
| , greetee = other.name || "Someone I don't know" | |
| ; | |
| console.log('Hello ' + greetee + ', my name is ' + me.name); | |
| }; | |
| Person.create = function (a, b, c) { | |
| return new Person(a, b, c); | |
| }; | |
| bob = new Person(); | |
| alice = Person.create('Alice'); | |
| bobby = Person('Bobby Tables'); // protection from throwing an e from a lack of thisness | |
| bob.greet(alice); | |
| }()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment