在这些示例中调用“this"有什么区别? [英] What is the difference in the invocation of 'this' in these examples?

查看:29
本文介绍了在这些示例中调用“this"有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读 Crockford 的JS:好的部分".他有两个使用这个的例子,我不明白为什么在一个例子中他使用 this 而在另一个例子中他使用 that.

I'm reading Crockford's 'JS: The Good Parts'. He has two examples using this and I don't understand why in one instance he uses this and in another he uses 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('&lt;&quot;&gt;'.deentify()); 

第二个例子:

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?那个例子和后面那个例子有什么区别?

Why can the first example access this directly? What is the difference between that example and the one that follows it?

推荐答案

当你做obj.f()时,this里面的函数f 将引用 obj.

When you do obj.f(), this inside the function f will refer to obj.

在第一个示例中,deentify() 在字符串上被调用.在该函数中,他只需要调用该函数的对象,即字符串,即 deentify() 函数中的 this 将要引用的对象.

In the first example, deentify() is called on a string. In that function, he only needs the object on which the function was called, the string, which is what this inside the deentify() function is going to refer to.

为什么我们需要那个

add1 函数需要以某种方式存储对原始 add 函数的引用.add1 不能使用this,因为它not被称为add.add1.这可以通过在 that 上创建一个闭包来克服,在这个闭包中他保存了对你在 (add() 上执行 curry() 的函数的引用在示例中).

The add1 function needs to store a reference to the original add function somehow. add1 cannot use this, because it is not called as add.add1. This is overcome by creating a closure over that, in which he saves the reference to the function you execute curry() on (add() in the example).

当您调用add.curry() 时,this 将引用add 函数.(因为你在 add 上调用了 curry()).由于 curry 函数内部的闭包,that 将保留其值,并且在调用 add1() 时仍会引用 add 函数.

When you call add.curry(), this will refer to the add function. (Because you called curry()on add). Due to the closure inside the curry function, that will keep its value and will still reference the add function when add1() is called.

如果在从 curry() 返回的函数中使用了 this,它将引用 window 对象.

If this was used inside the function returned from curry(), it would refer to the window object.

Function.method('curry', function() {
    var args = arguments, 
      that = this; //reference to add
    return function () {
        //`apply` calls add
        return that.apply(null, args.concat(arguments)); 
    };
});
var add1 = add.curry(1);
document.writeln(add1(6));

注意:重要的是要看到,第一个片段中的第一个 return 表示 deentify() 函数,而第二个片段中的第一个 return 表示 curry() 函数的返回值 .

Note: It is important to see, that the first return in the first snippet denotes the deentify() function, whereas the first return in the second snippet denotes the return value of the curry() function.

如果您想了解使 curry 起作用的 arguments/apply() 魔法,请在评论中提问,我很乐意详细说明.

If you want to understand the arguments/apply() magic that makes curry work as well, just ask in the comment, I'll be happy to elaborate.

这篇关于在这些示例中调用“this"有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆