简体   繁体   English

javascript私有和公共函数以及成员

[英]javascript private and public functions and members

I'm reading a small tutorial on javascript private members and public members ( http://www.crockford.com/javascript/private.html ) 我正在阅读有关javascript私有成员和公共成员的小型教程( http://www.crockford.com/javascript/private.html

it is confusing me though because here it says: 这让我感到困惑,因为它在这里说:

Public 上市

The members of an object are all public members. 对象的成员都是公共成员。 Any function can access, modify, or delete those members, or add new members. 任何功能都可以访问,修改或删除这些成员,或添加新成员。 There are two main ways of putting members in a new object: 将成员放入新对象的主要方法有两种:

In the constructor 在构造函数中

This technique is usually used to initialize public instance variables. 此技术通常用于初始化公共实例变量。 The constructor's this variable is used to add members to the object. 构造函数的this变量用于将成员添加到对象。

function Container(param) {
    this.member = param;
}

Then later it says: 然后,它说:

Private members are made by the constructor. 私有成员由构造函数组成。 Ordinary vars and parameters of the constructor becomes the private members. 构造函数的普通变量和参数成为私有成员。

function Container(param) {
    this.member = param;
    var secret = 3;
    var that = this;
}

"This constructor makes three private instance variables: param, secret, and that." “该构造函数生成三个私有实例变量:param,secret和that。”

I don't get it........ if a constructors parameters end up being private then why was that first example given as being public? 我不明白........如果一个构造函数参数最终是私有的,那为什么第一个例子是公开的呢?

在第一个示例中,将member创建为public成员,其值初始化为param的(否则为private)值。

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

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