¿Cuál es la diferencia en la invocación de 'esto' en estos ejemplos?
Estoy leyendo 'JS: The Good Parts' de Crockford. Él tiene dos ejemplos que usan esto y no entiendo por qué en un caso usathis
y en otro usathat
.
El primer ejemplo:
String.method('deentify', function() {
var entity = {
quot: '"',
lt: '<',
gt: '<'
};
return function() {
return this.replace(/&([^&;]+);/g,
function (a, b) {
var r = entity[b];
return typeof r === 'string' ? r : a;
}
);
};
}());
document.writeln('<">'.deentify());
El segundo ejemplo:
Function.method('curry', function() {
var args = arguments, that = this;
return function () {
return that.apply(null, args.concat(arguments));
};
});
var add1 = add.curry(1);
document.writeln(add1(6));
¿Por qué puede el primer ejemplo acceder?this
¿directamente? ¿Cuál es la diferencia entre ese ejemplo y el que lo sigue?