Last active
August 29, 2015 14:02
-
-
Save Williammer/030ea07d6d662826b9c7 to your computer and use it in GitHub Desktop.
jsPatterns.publicStatic.js
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
| // constructor | |
| var Gadget = function (price) { | |
| this.price = price; | |
| }; | |
| // a static method | |
| Gadget.isShiny = function () { | |
| // this always works | |
|  | |
| var msg = "you bet"; | |
| if (this instanceof Gadget) { | |
| // this only works if called non-statically | |
| msg += ", it costs $" + this.price + '!'; | |
| } | |
| return msg; | |
| }; | |
| // a normal method added to the prototype | |
| Gadget.prototype.isShiny = function () { | |
| return Gadget.isShiny.call(this); | |
| }; | |
| //output test | |
| Gadget.isShiny(); // "you bet" | |
| var a = new Gadget('499.99'); | |
| a.isShiny(); // "you bet, it costs $499.99!" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment