JavaScript Object-oriented private static variable case analysis _javascript techniques

Source: Internet
Author: User

This article analyzes the private static variables of JavaScript object-oriented. Share to everyone for your reference, specific as follows:

As you know, the principle of a private instance variable is based on the scope.

The private instance variable is implemented within the JavaScript function with the var keyword and is valid only within the function.

In imitation of this, the solution to the private static variable is presented:

<script language= "javascript" type= "Text/javascript" >
var jsclass = (function () {
 var privatestaticvariable = "private static variable";
 var privatestaticmethod = function () {
  alert ("Invoke private static Method");
 return function () {
  this.test1 = function () {return
   privatestaticvariable;
  }
  This.test2 = function (obj) {
   privatestaticvariable = obj;
  }
  This.test3 = function () {
   privatestaticmethod ();
  }
 };}
) ();
var testObject1 = new Jsclass ();
var testObject2 = new Jsclass ();
Alert (Testobject1.test1 ());
Testobject1.test2 ("changed private static variable");
Alert (Testobject2.test1 ());
Testobject2.test3 ();
</script>

Note that instead of directly defining a JavaScript class, you use an anonymous function as a container for static variables and return a JavaScript class .

For more information on JavaScript-oriented object-related content readers can view the site topics : "JavaScript Object-oriented introductory tutorial"

I hope this article will help you with JavaScript programming.

Related Article

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.