English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Deep Understanding of Block Scope, Private Variables, and Module Pattern in JavaScript

This article introduces in detail the block scope, private variables, and module pattern in JavaScript, without going into too much detail, as follows:

1.Block scope (private scope),It is often used outside of functions in the global scope, thus 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 
})(5); 
(function(){ 
  var now=new Date(); 
  if(now.getMonth()==0 && now.getDate()==1){ 
    console.log("Happy New Year"); 
  }else{ 
    console.log("Look forward to it"); 
  }; 
})(); 

 2.Private variables:Any variable defined within a function can be considered a private variable because these variables cannot be accessed outside the function.

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

2.1Define privileged methods 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 
console.log(person2.getName());//=>Alex 

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

2.2Static private variables to implement privileged methods

Firstly, define private variables and functions in the private scope, and then define the constructor and its public methods.

 (function(){ 
  //Private Variables and Functions 
  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 Pattern:Enhance the singleton by adding private variables and privileged methods.

If you need to create an object and initialize it with some data, and also need to make some methods public to access these private data, then you can use the module pattern.

var application=function(){ 
  //Private Variables and Functions 
  var components=[]; 
  //Initialization 
  components.push(new BaseComponent()); 
  //Public Interface 
  return { 
    getComponentCount:function(){ 
      return components.length; 
    , 
    registerComponent:function(){ 
      if(typeof component=="object"){ 
        components.push(component); 
      }; 
    }; 
  }; 
}; 

That's all for this article. I hope it will be helpful to everyone's learning and that everyone will support the Yelling Tutorial more.

Declaration: The content of this article is from the Internet, and the copyright belongs to the original author. The content is contributed and uploaded by Internet users spontaneously. This website does not own the copyright, has not been manually edited, and does not assume any relevant legal liability. If you find any content suspected of copyright infringement, please send an email to notice#w3Please send an email to codebox.com (replace # with @ when sending an email) to report any violations, and provide relevant evidence. Once verified, this site will immediately delete the content suspected of infringement.

You May Also Like