Static private variables for JS simulation

Source: Internet
Author: User

JavaScript static variables and instance variables Blog Category:
    • Javascript

Strictly speaking, JS inside there is no static and private concept, all member properties are public, the following is only reference Oo language, in some way to achieve similar concepts.

One. static variables

1. private static variables

All instances are shared. Because it is a private variable, it cannot be accessed directly by the function name. implemented by closures.

According to the scope principle of the closure, it can only be accessed within the closure package. Therefore, it is not possible to access this static variable anywhere in the class. Only constructors or methods of the class (the prototype method) can be accessed within the closure.

JS Code
  1. (function () {
  2. var privatestatic = "Privatestatic";
  3. Func = function () {
  4. this.setprivatestatic = function (value) {
  5. Privatestatic = value;
  6. }
  7. this.getprivatestatic = function () {
  8. return privatestatic;
  9. }
  10. }
  11. })();
  12. var func1 = new Func ();
  13. var func2 = new Func ();
  14. Console.log (Func1.getprivatestatic ()); //Privatestatic
  15. Console.log (Func2.getprivatestatic ()); //Privatestatic
  16. Console.log (func1.setprivatestatic (' changed '));
  17. Console.log (Func2.getprivatestatic ()); //changed

2. public static variables

This is relatively straightforward and directly defines the properties of the function.

Backbone's extend function has two parameters, the first argument is an instance variable, and the second argument is a static variable. This is how the interim static variable is implemented.

JS Code
    1. Func = function () {
    2. this.test = ' Test ';
    3. }
    4. func.acfun= ' net ';
    5. Console.log (Func.acfun); //net

Two. Instance variable

1. Private instance variables

Within the constructor, variables defined by VAR are private instance variables that can be accessed only within the construct.

JS Code
  1. var person = Function (value) {
  2. var age =value;
  3. This.getage = function () {
  4. return age;
  5. }
  6. }
  7. Person.prototype._getage = function () {
  8. return age;
  9. }
  10. var yaoming = New Person (' 27 ');
  11. Console.log (yaoming.age) //undefined
  12. Console.log (Yaoming.getage ()) //27
  13. Console.log (Yaoming._getage ()) //defined, the prototype method cannot access age, and only private instance variables can be accessed inside the constructor

2. Public instance variables

Use the properties defined in the constructor or prototype method, as well as the properties defined in the prototype.

The constructor is instantiated with the new operator, a new object is created, the scope of the constructor is assigned to the new object, and the code executes, and if there is this definition property or method in the constructor, the property or method is added to the new object.

JS Code
      1. var person = Function (name) {
      2. this.name = name;
      3. }
      4. Person.prototype.age = ' 11 ';
      5. var yaoming = New Person (' ym ');
      6. Console.log (Yaoming.name);
      7. Console.log (Yaoming.age); //11

Static private variables for JS simulation

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.