JavaScript作用域

1.块级作用域

(function(){     
  //这里是块级作用域 
})(); 

2.私有变量

//1
function MyObject(){ 
  //私有变量和私有函数     
  var privateVariable = 10; 
  function privateFunction(){         
    return false;     
 } 
  //特权方法     
  this.publicMethod = function (){         
    privateVariable++;         
    return privateFunction(); 
  }; 
} 
//2
function Person(name){
  this.getName = function(){         
    return name;     
  }; 
  this.setName = function (value) {
    name = value;     
 }; 
} 
 
var person = new Person("Nicholas"); 
alert(person.getName());   //"Nicholas" 
person.setName("Greg"); 
alert(person.getName());   //"Greg" 

3.静态私有变量

//通过在私有作用域中定义私有变量或函数,同样也可以创建特权方法,其基本模式如下所示。
//1
(function(){          
  //私有变量和私有函数     
  var privateVariable = 10; 
  function privateFunction(){
    return false;     
  } 
  //构造函数     
  MyObject = function(){}; 
  //公有/特权方法     
  MyObject.prototype.publicMethod = function(){
    privateVariable++;         
    return privateFunction();     
  }; 
})(); 
//2
 (function(){          
   var name = "";
   Person = function(value){         
     name = value;     
   };          
   Person.prototype.getName = function(){         
     return name;     
   };          
   Person.prototype.setName = function (value){ 
           name = value;     
   }; 
 })(); 
 
var person1 = new Person("Nicholas"); 
alert(person1.getName());  //"Nicholas" 
person1.setName("Greg"); 
alert(person1.getName());  //"Greg" 

var person2 = new Person("Michael"); 
alert(person1.getName()); //"Michael" 
alert(person2.getName()); //"Michael" 

4.模块模式

var singleton = {     
  name : value,     
  method : function () {         
    //这里是方法的代码     
  } 
}; 
//模块模式通过为单例添加私有变量和特权方法能够使其得到增强,其语法形式如下: 
var singleton = function(){          
  //私有变量和私有函数     
  var privateVariable = 10;          
  function privateFunction(){         
    return false;     
  } 
   //特权/公有方法和属性     
  return { 
    publicProperty: true, 
    publicMethod : function(){             
      privateVariable++;            
      return privateFunction();         
    } 
  }; 
}(); 
//这种模式在需要对单例进行某些初始化,同时又需要维护其私有 变量时是非常有用的,例如:
var application = function(){ 
  //私有变量和函数
  var components = new Array(); 
  //初始化     
  components.push(new BaseComponent()); 
  //公共     
  return {         
    getComponentCount : function(){             
      return components.length;         
    }, 
    registerComponent : function(component){             
      if (typeof component == "object"){
        components.push(component);            
      }         
    }     
  }; 
}(); 
/*
在 Web 应用程序中,经常需要使用一个单例来管理应用程序级的信息。这个简单的例子创建了一 个用于管理组件的 application 对象。在创建这个对象的过程中,首先声明了一个私有的 components 数组,并向数组中添加了一个 BaseComponent 的新实例(在这里不需要关心 BaseComponent 的代码,我 们只是用它来展示初始化操作)。而返回对象的 getComponentCount()和registerComponent()方法,都 是有权访问数组 components 的特权方法。前者只是返回已注册的组件数目,后者用于注册新组件。 简言之,如果必须创建一个对象并以某些数据对其进行初始化,同时还要公开一些能够访问这些私有 数据的方法,那么就可以使用模块模式。以这种模式创建的每个单例都是 Object 的实例,因为终要通 过一个对象字面量来表示它。事实上,这也没有什么;毕竟,单例通常都是作为全局对象存在的,我们不 会将它传递给一个函数。因此,也就没有什么必要使用 instanceof 操作符来检查其对象类型了
*/

5.增强的模块模式

var singleton = function(){ 
  //私有变量和私有函数     
  var privateVariable = 10; 
  function privateFunction(){         
    return false;     
  } 
  //创建对象     
  var object = new CustomType(); 
  //添加特权/公有属性和方法     
  object.publicProperty = true;
  object.publicMethod = function(){         
    privateVariable++;         
    return privateFunction();     
  }; 
  //返回这个对象     
  return object; 
}(); 
 //如果前面演示模块模式的例子中的 application 对象必须是 BaseComponent 的实例,那么就可 以使用以下代码。 
var application = function(){ 
  //私有变量和函数     
  var components = new Array(); 
  //初始化     
  components.push(new BaseComponent()); 
  //创建 application 的一个局部副本    
  var app = new BaseComponent(); 
  //公共接口     
  app.getComponentCount = function(){         
    return components.length;     
  }; 
  app.registerComponent = function(component){         
    if (typeof component == "object"){
      components.push(component);         
    }     
  }; 
  //返回这个副本
   return app; 
}(); 

你可能感兴趣的:(JavaScript作用域)