クロックフォードの「JS: The Good Parts」を読んでいます。彼はこれを使用した 2 つの例を持っていthis
ますthat
。
最初の例:
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());
2 番目の例:
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));
this
最初の例で直接アクセスできるのはなぜですか? その例とそれに続く例の違いは何ですか?