Visibility of object members in Javascript

Source: Internet
Author: User
Similar to the usage of keywords such as private, protected, and public in class-based object-oriented languages such as Java, the object-based JavaScript language also has similar member visibility issues in object construction.

The visibility definitions of JavaScript Object structures can be divided into the following types:

1. Private Properties)
  
The VaR keyword defines the scope of the variable in the object construction. The variable can only be accessed within the scope of the object construction method. For example:

Function variabletest ()
{
VaR myvariable; // Private
}

VaR VT = new variabletest ();
Vt. myvariable; // The undefined exception occurs.

2. Private methods)

Similar to private attributes, the object can only be accessed within the scope of the object constructor. For example:

Code
Function methodtest ()
{
VaR mymethod = function () // Private
{
Alert ("Private method ");
}

This. Invoke = function ()
{
// Access mymethod
Mymehtodd ();
}
}

VaR Mt = new methodtest ();
Mt. mymethod (); // error. If you use trycatch, you can catch an exception in "The object does not support this attribute or method ".
Mt. Invoke ();

3. Public Properties)

There are two ways to define public attributes:

(1) defined by the this keyword. For example:

Function privilegedvariable ()
{
This. Variable = "privileged variable ";
}

VaR Pv = new privilegedvariable ();
PV. variable; // return "privileged variable"

(2) It is defined by the prototype of the constructor. For example:

Function publicvariable (){}
Publicvariable. Prototype. Variable = "Public variable ";
VaR Pv = new publicvariable ();
PV. variable; // return "Public variable"

4. Public Methods)

Likewise, there are two ways to define public methods.
  
(1) defined by the this keyword. (2) It is defined by the prototype of the constructor.

It is omitted here ...........

5. Static Properties)

Attributes directly added for the object constructor cannot be accessed by the object instance and can only be used by the constructor itself. For example:

Code
Function staticvariable (){}
Staticvariable. Variable = "static variable ";

VaR SV = new staticvariable ();
Sv. variable; // return "undefined"
Staticvariable. Prototype. variable; // return "undefined"
Staticvariable. variable; // return "static variable"

6. Static Methods)

Methods directly added for the object constructor cannot be accessed by the object instance and can only be used by the constructor itself.

Code omitted ........

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.