Last active
December 5, 2017 03:27
-
-
Save mchelen/ff2ad2c4ae2b0b471b6f70fc77edeb4c to your computer and use it in GitHub Desktop.
how to access a parent property from inside IIFE
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
// 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