Deep understanding of block-level scopes, private variables, and module patterns in JavaScript basics

Source: Internet
Author: User

This article introduces the block-level scope, the private variable and the module mode in JavaScript in detail, and the nonsense is not much said, as follows:

1. Block-level scopes (private scopes) are often used outside of functions in global scope, limiting the addition of too many variables and functions to the global scope.

(function (count) {for 
  (var i=0;i<count;i++) { 
    console.log (i);//=>0, 1, 2, 3, 4 
  } 
  console.log (i) ;//=>5 
(function () { 
  var now=new Date (); 
  if (Now.getmonth () ==0 && now.getdate () ==1) { 
    console.log ("Happy New Year"); 
  } else{ 
    Console.log ("to the fullest expectations"); 
  } 

2. Private variable: any variable defined in a function can be considered a private variable because it cannot be accessed outside the function.

Privileged methods: Public methods that have access to private variables and private functions are called privileged methods.

2.1 Define the privileged method in the constructor:

 function person (name) { 
  this.getname=function () {return 
    name; 
  }; 
  This.setname=function (value) { 
    name=value;} 
  ; 
} 
var person1=new person ("Jason"); 
Console.log (Person1.getname ());//=>jason 
person1.setname ("Gray") 
; Console.log (Person1.getname ());//=>gray 
var person2=new person ("Michael") 
; Console.log (Person1.getname ());//=>gray 
Console.log (Person2.getname ());//=>michael 
Person2.setname (' Alex '); 
Console.log (Person1.getname ());//=>gray 

The disadvantage of a constructor pattern is that you create the same set of new methods for each instance.

2.2 Static private variables to implement privileged methods

In a private scope, you first define a private variable and a private function, and then define the constructor and its public methods.

 (function () { 
  //private variable and function 
  var name= ""; 
  Person=function (value) { 
    name=value; 
  }; 
  Privileged method 
  Person.prototype.getname=function () {return 
    name; 
  }; 
  Person.prototype.setname=function (value) { 
    name=value; 
  } 
}) (); 
var person1=new person ("Jason"); 
Console.log (Person1.getname ());//=>jason 
person1.setname ("Gray") 
; Console.log (Person1.getname ());//=>gray 
var person2=new person ("Michael") 
; Console.log (Person1.getname ());//=>michael 
Console.log (Person2.getname ());//=>michael 
Person2.setname (' Alex '); 
Console.log (Person1.getname ());//=>alex 
Console.log (Person2.getname ());//=>alex 

3. Module mode: It can be enhanced by adding private variables and privileged methods for a single instance.

You can use module mode if you have to create an object and initialize it with some data, and expose some ways to access the private data.

var application=function () { 
  //private variable and function 
  var components=[]; 
  Initialize 
  Components.push (new Basecomponent ()); 
  Public interface return 
  { 
    getcomponentcount:function () {return 
      components.length; 
    }, 
    Registercomponent:function () { 
      if (typeof component== "Object") { 
        Components.push (component); 
      } 
    } 
  } 
} (); 

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.