Last active
August 29, 2015 13:57
-
-
Save chrisslater/9399623 to your computer and use it in GitHub Desktop.
Force a constructor method if 'new' isn't used.
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 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