这些“这个"是做什么的?在这段 JavaScript 代码中是什么意思? [英] What do these "this" mean in this JavaScript code?

查看:44
本文介绍了这些“这个"是做什么的?在这段 JavaScript 代码中是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
    <head>
        <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
        <title>test</title>

    </head>
    <body>
        <script type="text/javascript" charset="utf-8">
            (function(){
                // this
                var test=function(){
                    //this
                    return function(){
                        //this
                    };
                }

                (function(){
                    //this
                    var a={
                        p1:function(){
                            //this
                        }
                    };
                })();
            })();
        </script>       
    </body>
</html>

推荐答案

David Dorward 已经提到了 JavaScript: The Good Parts by Douglas Crockford.

David Dorward already mentioned about JavaScript: The Good Parts by Douglas Crockford.

来自那本优秀书籍的第 4.3 节:

From Section 4.3 of that excellent book:

调用一个函数会挂起执行当前函数,将控制和参数传递给新功能.除了声明的参数,每个函数接收两个附加参数:这和论点.这个参数在面向对象中非常重要编程,它的价值是由调用模式决定.有四种调用模式在 JavaScript 中:方法调用模式,函数调用模式,构造函数调用模式和应用调用图案.模式的不同之处在于这是奖金参数已初始化.

Invoking a function suspends the execution of the current function, passing control and parameters to the new function. In addition to the declared parameters, every function receives two additional parameters: this and arguments. The this parameter is very important in object oriented programming, and its value is determined by the invocation pattern. There are four patterns of invocation in JavaScript: the method invocation pattern, the function invocation pattern, the constructor invocation pattern, and the apply invocation pattern. The patterns differ in how the bonus parameter this is initialized.

Crockford 继续解释每个模式中this"的绑定,如下所示:

Crockford continues to explains the binding of 'this' in each of these patterns, as follows:

方法调用模式:当函数作为对象的属性存储时,我们称其为方法.当一个方法被调用时,this 绑定到那个对象.

The Method Invocation Pattern: When a function is stored as a property of an object, we call it a method. When a method is invoked, this is bound to that object.

函数调用模式:当使用此模式调用函数时, this 绑定到全局对象.这是语言设计的一个错误.

The Function Invocation Pattern: When a function is invoked with this pattern, this is bound to the global object. This was a mistake in the design of the language.

构造函数调用模式:如果使用 new 前缀调用函数,则将创建一个新对象,该对象带有指向函数原型成员值的隐藏链接,并将绑定该对象到那个新对象.

The Constructor Invocation Pattern: If a function is invoked with the new prefix, then a new object will be created with a hidden link to the value of the function's prototype member, and this will be bound to that new object.

Apply 调用模式: apply 方法让我们构造一个参数数组,用于调用函数.它还让我们选择 this 的值.apply 方法有两个参数.第一个是应该绑定到 this 的值.第二个是参数数组.

The Apply Invocation Pattern: The apply method lets us construct an array of arguments to use to invoke a function. It also lets us choose the value of this. The apply method takes two parameters. The first is the value that should be bound to this. The second is an array of parameters.

这篇关于这些“这个"是做什么的?在这段 JavaScript 代码中是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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