-
-
Save ChazAttack73/819701ea13bfd3532914 to your computer and use it in GitHub Desktop.
Module Pattern - How to avoid using `this` and When you need to use `this`
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
module.exports = (function() { | |
var name = 'Ray'; | |
var age = 32; | |
function doSomethingGreat() { | |
console.log(name); | |
console.log(age); | |
anotherGreatness(); | |
} | |
function anotherGreatness() { | |
console.log(name); | |
console.log(age); | |
doSomething(); | |
} | |
return { | |
doSomethingGreat: doSomethingGreat, | |
anotherGreatness: anotherGreatness, | |
name: name, | |
age: age | |
}; | |
})(); |
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
module.exports = (function() { | |
var foo = { | |
name: "Ray", | |
age: 32, | |
doSomethingGreat: function () { | |
console.log(foo.name); | |
console.log(foo.age); | |
}, | |
anotherGreatness: function() { | |
console.log(foo.name); | |
console.log(foo.age); | |
foo.doSomethingGreat(); | |
} | |
} | |
return foo; | |
})(); |
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
module.exports = (function() { | |
return { | |
name: "Ray", | |
age: 32, | |
doSomethingGreat: function () { | |
console.log(this.name); | |
console.log(this.age); | |
}, | |
anotherGreatness: function() { | |
console.log(this.name); | |
console.log(this.age); | |
this.doSomethingGreat(); | |
} | |
}; | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment