Created
December 4, 2014 19:53
-
-
Save topicus/48fc347cd45c76ed0615 to your computer and use it in GitHub Desktop.
Explicación de this en javascript
This file contains 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
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