JS:private variables

来源:互联网 发布:西安航海家软件 编辑:程序博客网 时间:2024/05/20 22:37

  • -Public properties can be accessed from outside the class
  • -Private properties can only be accessed from within the class
Using constructor notation, a property declared as this.property ="someValue" will be public, whereas a property declared with var property ="hiddenValue" will be private.

Just as functions can have local variables which can only be accessed from within that function, objects can have private variables. Privatevariables are pieces of information you do not want to publicly share, and they can only be directly accessed from within the class.

The Person class has been modified to have a private variable calledbankBalance. Notice that it looks just like a normal variable, but it is defined inside the constructor for Personwithout using this, but instead usingvar. This makes bankBalance a private variable.

0 0
原创粉丝点击