Search code examples
javascriptprivate-members

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)

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.

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."

I don't get it........ if a constructors parameters end up being private then why was that first example given as being public?


Solution

  • In the first example, member is created as a public member with its value initialized to the (otherwise private) value of param.