简体   繁体   English

Javascript 私有方法

[英]Javascript Private Methods

I was reading chapter 2 of Apres Javascript Pro techniques and in particular the section about Provate Methods .我正在阅读 Apres Javascript Pro 技术的第 2 章,特别是关于Provate Methods的部分。

The following code snippet is shown as an example:以下代码片段作为示例显示:

// 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();
}

Apart the error at line 4, when i create a new Classroom object,除了第 4 行的错误,当我创建一个新的教室 object 时,

var class = new Classroom(["Jhon", "Bob"], "Mr. Smith");

the following error is thrown:抛出以下错误:

Uncaught TypeError: Cannot call method 'join' of undefined.

Reading at douglas.crockford.com/private.html, I found this:在 douglas.crockford.com/private.html 阅读,我发现了这个:

By convention, we make a private that variable.按照惯例,我们将那个变量设为私有。 This is used to make the object available to the private methods.这用于使 object 可用于私有方法。 This is a workaround for an error in the ECMAScript Language Specification which causes this to be set incorrectly for inner functions.这是 ECMAScript 语言规范中错误的解决方法,该错误导致内部函数的设置不正确。

Indeed creating a that variable pointing to this , the previous code work as expected.确实创建了一个指向this变量,前面的代码按预期工作。

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();             
}

So my question is:所以我的问题是:

  • It is always necessary to create a that variable?总是需要创建一个那个变量?

If yes, this means that the example was definitively wrong.如果是,这意味着该示例绝对是错误的。

It is only necessary to store the value of this into another variable that if you for some reason want to keep the value that this had when calling the outer method.只需将this的值存储到另一个变量that如果您出于某种原因想要保留this在调用外部方法时的值。

The error you get (Uncaught TypeError: Cannot call method 'join' of undefined.) means that the property names was not found on the this object and that the value is therefore undefined and consequently cannot have a names property.您得到的错误(Uncaught TypeError: Cannot call method 'join' of undefined.)意味着在this object 上找不到属性names ,因此该值undefined ,因此不能具有names属性。

The value of this in JavaScript is a little complicated to know. JavaScript 中this值的了解有点复杂。 If you invoke a function f as a method , that is if you write of() then this is bound to o inside the function f .如果您将 function f作为方法调用,也就是说,如果您编写of()this将绑定到 function f内部的o If you call f as a function , that is f() then this is bound to the global (window) object (.).如果您将f称为function ,即f() ,那么this将绑定到全局(窗口)object (.)。

Therefore, if you change the last line disp();因此,如果您更改最后一行disp(); to this.disp();this.disp(); , then this will be what you expect inside disp . ,那么this将是您在disp中所期望的。

The code is indeed wrong...代码确实错了……

this refers to the owner of a function (window object, HTML element...) so in a private function you won't be able to access the object you're working on. this refers to the owner of a function (window object, HTML element...) so in a private function you won't be able to access the object you're working on. so you store the object in that variable, so you can access it from any private method in the class.因此您将 object 存储在that变量中,因此您可以从 class 中的任何私有方法访问它。

Your first example has another error in that you're not defining this.names, but the answer to your question is basically 'yes' - inside the disp function body the 'this' variable is assigned to the global scope, so you need to create the 'that' variable.您的第一个示例有另一个错误,即您没有定义 this.names,但您的问题的答案基本上是“是” - 在 disp function 主体内,“this”变量被分配给全局 scope,所以您需要创建“那个”变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM