Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/http/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript私有方法_Javascript_Private Methods - Fatal编程技术网

Javascript私有方法

Javascript私有方法,javascript,private-methods,Javascript,Private Methods,我正在阅读Apres Javascript Pro技术的第2章,特别是关于验证方法的部分 以下代码段显示为示例: // Listing 2-23. Example of a Private Method Only Usable by the Constructor Function function Classroom(students, teacher) { // A private method for displaying all the students in the class

我正在阅读Apres Javascript Pro技术的第2章,特别是关于验证方法的部分

以下代码段显示为示例:

// Listing 2-23. Example of a Private Method Only Usable by the Constructor Function
function Classroom(students, teacher) {
    // A private method for displaying all the students in the class
    function disp() {
       alert(this.names.join(", "));  // i think here there is an error. Should be alert(this.students.join(", "));
    }

    // Store the class data as public object properties
    this.students = students;
    this.teacher  = teacher;

    disp();
}
除了第4行的错误,当我创建一个新的教室对象时

var class = new Classroom(["Jhon", "Bob"], "Mr. Smith");
将引发以下错误:

Uncaught TypeError: Cannot call method 'join' of undefined.
阅读douglas.crockford.com/private.html,我发现:

按照惯例,我们将该变量设为私有变量。这用于使对象对私有方法可用。这是ECMAScript语言规范中错误的解决方法,该错误导致内部函数的错误设置

实际上,前面的代码创建了一个指向此变量的变量,并按预期工作

function Classroom(students, teacher) {
    var that;
    // A private method used for displaying al the students in the class
    function disp() {
        alert(that.students.join(", "));
    }

    // Store the class data as public object properties
    [...]   
    that = this;

    disp();             
}
所以我的问题是:

总是有必要创建一个变量吗?
如果是,这意味着示例完全错误。

这是指函数窗口对象的所有者,HTML元素。。。因此,在私有函数中,您将无法访问正在处理的对象。因此,您将对象存储在该变量中,以便可以从类中的任何私有方法访问它。

这是指函数窗口对象HTML元素的所有者。。。因此,在私有函数中,您将无法访问正在处理的对象。因此,您将对象存储在该变量中,以便可以从类中的任何私有方法访问它。

如果出于某种原因希望保留调用外部方法时的值,则只需将该值存储到另一个变量中

未捕获的错误TypeError:无法调用未定义的方法“join”。表示在此对象上未找到属性名称,因此该值未定义,因此不能具有names属性

这在JavaScript中的价值有点复杂。如果您将函数f作为方法调用,也就是说如果您编写o.f,那么它将绑定到函数f中的o。如果将f作为函数调用,即f,则它将绑定到全局窗口对象

因此,如果您更改最后一行disp;对于此.disp;,那么这就是您在disp中所期望的


代码确实是错误的…

如果出于某种原因希望保留调用外部方法时的值,则只需要将此值存储到另一个变量中

未捕获的错误TypeError:无法调用未定义的方法“join”。表示在此对象上未找到属性名称,因此该值未定义,因此不能具有names属性

这在JavaScript中的价值有点复杂。如果您将函数f作为方法调用,也就是说如果您编写o.f,那么它将绑定到函数f中的o。如果将f作为函数调用,即f,则它将绑定到全局窗口对象

因此,如果您更改最后一行disp;对于此.disp;,那么这就是您在disp中所期望的


代码确实是错误的…

您的第一个示例还有一个错误,即您没有定义this.names,但您的问题的答案基本上是“是”-在disp函数体中,“this”变量被分配到全局范围,因此,您需要创建“that”变量。

您的第一个示例还有一个错误,即您没有定义this.names,但问题的答案基本上是“yes”-在disp函数体中,“this”变量被分配到全局范围,因此您需要创建“that”变量。

如果这些错误在第2章中,我害怕在后面的章节中看到代码示例!如果这些错误在第2章中,我害怕在后面的章节中看到代码示例!我认为那行不通。在示例中,代码disp不是教室的成员,因此this.disp将抛出一个正确的错误。Disp还必须是教室的一员。该代码在很多方面都是错误的…:-我认为那行不通。在示例中,代码disp不是教室的成员,因此this.disp将抛出一个正确的错误。Disp还必须是教室的一员。该代码在很多方面都是错误的…:-