Skip to content

Instantly share code, notes, and snippets.

@topicus
Created December 4, 2014 19:53
Show Gist options
  • Save topicus/48fc347cd45c76ed0615 to your computer and use it in GitHub Desktop.
Save topicus/48fc347cd45c76ed0615 to your computer and use it in GitHub Desktop.
Explicación de this en javascript
function MiConstructor(){
scopeGlobal = 10;
var scopeLocal = 11;
var miArray = [2,10,2,3];
// apunta correctamente a MiConstructor
console.log(this);
this.mimetodo = function(){
// apunta correctamente a MiConstructor
console.log(this);
miArray.forEach(function(item){
// oops. ahora apunta a window
console.log(this);
});
}
}
var ct = new MiConstructor();
ct.mimetodo();
console.log(window.scopeGlobal);
console.log(window.scopeLocal);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment