Skip to content

Instantly share code, notes, and snippets.

@chrisslater
Last active August 29, 2015 13:57
Show Gist options
  • Save chrisslater/9399623 to your computer and use it in GitHub Desktop.
Save chrisslater/9399623 to your computer and use it in GitHub Desktop.
Force a constructor method if 'new' isn't used.
function Podcast(title, url) {
// Forces the new instance so that 'this' has context inside Podcast
if(false === (this instanceof Podcast)) {
return new Podcast(title, url);
}
this.title = title;
this.url = url;
this.toString = function() {
return 'Title: ' + this.title;
}
}
var podcast1 = new Podcast('Astronomy cast', 'http:// ...');
var podcast2 = Podcast('jQuery podcast', 'http:// ...');
console.log(podcast1, podcast2);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment