Skip to content

Instantly share code, notes, and snippets.

@Williammer
Last active August 29, 2015 14:02
Show Gist options
  • Select an option

  • Save Williammer/030ea07d6d662826b9c7 to your computer and use it in GitHub Desktop.

Select an option

Save Williammer/030ea07d6d662826b9c7 to your computer and use it in GitHub Desktop.
jsPatterns.publicStatic.js
// 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