Skip to content

Instantly share code, notes, and snippets.

@ChazAttack73
Forked from sgnl/0-avoiding-this.js
Created January 9, 2016 04:18
Show Gist options
  • Save ChazAttack73/819701ea13bfd3532914 to your computer and use it in GitHub Desktop.
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`
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
};
})();
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;
})();
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