El comportamiento de "esto" cuando se llama a la función bar
me desconcierta. Vea el código a continuación. ¿Hay alguna manera de organizar "esto" para que sea una simple instancia de objeto js antiguo cuando se llama a la barra desde un manejador de clics, en lugar de ser el elemento html?jQuery/JavaScript "this" puntero confusión
// a class with a method
function foo() {
this.bar(); // when called here, "this" is the foo instance
var barf = this.bar;
barf(); // when called here, "this" is the global object
// when called from a click, "this" is the html element
$("#thing").after($("<div>click me</div>").click(barf));
}
foo.prototype.bar = function() {
alert(this);
}
Por favor explique el '" this "es la instancia de foo'. El siguiente jsfiddle (http://jsfiddle.net/yY6fp/1/) demuestra que 'this' en' this.bar() 'se evalúa como el objeto' window' (global). –