Skip to content

Instantly share code, notes, and snippets.

@LeandrodeLimaC
Last active August 20, 2021 23:50
Show Gist options
  • Save LeandrodeLimaC/c3909b16cf6ea648b2c7a134892f0838 to your computer and use it in GitHub Desktop.
Save LeandrodeLimaC/c3909b16cf6ea648b2c7a134892f0838 to your computer and use it in GitHub Desktop.
// Constructors
// Be careful when using
// Constructor pattern
function C() {
this.instanceMember = 'Whoops!!'
}
const c = C() // Forgot 'new'
c // <· undefined
window.instanceMember // <· 'Whoops!!'
// Always check if this is an
// instance of the contructor
function C() {
if(!(this instanceof C))
return new C()
this.instanceMember = 'This is fine'
}
const c = C() // Is this a constructor?
c // <· { instanceMember: 'This is fine' }
window.instanceMember // <· undefined
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment