Skip to content

Instantly share code, notes, and snippets.

@mchelen
Last active December 5, 2017 03:27
Show Gist options
  • Save mchelen/ff2ad2c4ae2b0b471b6f70fc77edeb4c to your computer and use it in GitHub Desktop.
Save mchelen/ff2ad2c4ae2b0b471b6f70fc77edeb4c to your computer and use it in GitHub Desktop.
how to access a parent property from inside IIFE
// example of using .call with an IIFE
function myObject (text) {
this.text = text;
}
myObject.prototype.print = function () {
console.log(this.text); // works
(function() {
console.log("hello"); // works
console.log(this.text); // undefined
})();
(function() {
console.log("hello"); // works
console.log(this.text); // works
}).call(this);
}
var object = new myObject("world");
object.print();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment