Last active
August 20, 2021 23:50
-
-
Save LeandrodeLimaC/c3909b16cf6ea648b2c7a134892f0838 to your computer and use it in GitHub Desktop.
This file contains 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
// 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