Skip to content

Instantly share code, notes, and snippets.

@pr00thmatic
Last active August 29, 2015 14:15
Show Gist options
  • Save pr00thmatic/5208b9218df9de81e527 to your computer and use it in GitHub Desktop.
Save pr00thmatic/5208b9218df9de81e527 to your computer and use it in GitHub Desktop.
var a = (function () {
// "this" se refiere a la función instantánea anónima
this.x = 100;
var a = 1;
var showX = function () {
// el valor de "this" depende de quién
// invoque a esta función
console.log(this.x);
};
var setX = function () {
// el valor de "this" depende de quién
// invoque a esta función
this.x = -100;
};
showX(); // 100
// como el valor de "this" en showX depende de quién
// lo invoque, en este execution context, "this" se
// refiere a la función anónima instantánea.
return {
a : a,
setX : setX,
showX : showX
};
})();
console.log(a.x); // undefined
a.setX();
// como el valor de "this" en showX depende de quién
// lo invoque, en este execution context, "this" se
// refiere a la variable a.
console.log(a.x); // -100
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment