欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

深入理解JavaScript中的塊級(jí)作用域、私有變量與模塊模式

 更新時(shí)間:2016年10月31日 08:43:14   作者:bboyjoe  
本篇文章詳細(xì)的介紹了JavaScript中的塊級(jí)作用域、私有變量與模塊模式,詳細(xì)介紹了塊級(jí)作用域、私有變量與模塊模式,對(duì)學(xué)習(xí)JavaScript很有幫助。

本文詳細(xì)的介紹了JavaScript中的塊級(jí)作用域、私有變量與模塊模式,廢話就不多說了,具體如下:

1.塊級(jí)作用域(私有作用域),經(jīng)常在全局作用域中被用在函數(shù)外部,從而限制向全局作用域中添加過多的變量和函數(shù)。

(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("新年快樂"); 
  }else{ 
    console.log("盡情期待"); 
  } 
})(); 

 2.私有變量:任何在函數(shù)中定義的變量,都可以認(rèn)為是私有變量,因?yàn)椴荒茉诤瘮?shù)的外部訪問這些變量。

特權(quán)方法:有權(quán)訪問私有變量和私有函數(shù)的公有方法稱為特權(quán)方法。

2.1)在構(gòu)造函數(shù)中定義特權(quán)方法:

 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 

構(gòu)造函數(shù)模式的缺點(diǎn)是針對(duì)每個(gè)實(shí)例都會(huì)創(chuàng)建同樣一組新方法。

2.2)靜態(tài)私有變量來實(shí)現(xiàn)特權(quán)方法

在私有作用域中,首先定義私有變量和私有函數(shù),然后定義構(gòu)造函數(shù)及其公有方法。

 (function(){ 
  //私有變量和函數(shù) 
  var name=""; 
  Person=function(value){ 
    name=value; 
  }; 
  //特權(quán)方法 
  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.模塊模式:通過為單例添加私有變量和特權(quán)方法能夠使其得到增強(qiáng)。

如果必須創(chuàng)建一個(gè)對(duì)象并以某些數(shù)據(jù)對(duì)其進(jìn)行初始化,同時(shí)還要公開一些能夠訪問這些私有數(shù)據(jù)的方法,那么就可以使用模塊模式。

var application=function(){ 
  //私有變量和函數(shù) 
  var components=[]; 
  //初始化 
  components.push(new BaseComponent()); 
  //公共接口 
  return { 
    getComponentCount:function(){ 
      return components.length; 
    }, 
    registerComponent:function(){ 
      if(typeof component=="object"){ 
        components.push(component); 
      } 
    } 
  } 
}(); 

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • JavaScript For...In 使用方法

    JavaScript For...In 使用方法

    JavaScript For...In 使用方法...
    2007-04-04
  • 用htc組件制作windows選項(xiàng)卡

    用htc組件制作windows選項(xiàng)卡

    用htc組件制作windows選項(xiàng)卡...
    2007-01-01
  • JavaScript 學(xué)習(xí)筆記一些小技巧

    JavaScript 學(xué)習(xí)筆記一些小技巧

    JavaScript 學(xué)習(xí)筆記一些小技巧,大家可以參考下,比較實(shí)用的知識(shí)。
    2010-03-03
  • javascript 基礎(chǔ)篇2 數(shù)據(jù)類型,語(yǔ)句,函數(shù)

    javascript 基礎(chǔ)篇2 數(shù)據(jù)類型,語(yǔ)句,函數(shù)

    文章里如果有錯(cuò)誤的話,希望能幫忙指正~我也是邊看視頻邊學(xué)習(xí)中,這個(gè)算是個(gè)筆記吧~自認(rèn)為總結(jié)出來的東西比看視頻要節(jié)省點(diǎn)時(shí)間~能幫到別人最好了~幫不到也起碼恩能幫到我自己
    2012-03-03
  • JavaScript中l(wèi)et避免閉包造成問題

    JavaScript中l(wèi)et避免閉包造成問題

    這篇文章主要介紹了JavaScript中l(wèi)et避免閉包造成問題,本篇文章通過簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下
    2021-07-07
  • 簡(jiǎn)介JavaScript中setUTCSeconds()方法的使用

    簡(jiǎn)介JavaScript中setUTCSeconds()方法的使用

    這篇文章主要介紹了簡(jiǎn)介JavaScript中setUTCSeconds()方法的使用,是JS入門學(xué)習(xí)中的基礎(chǔ)知識(shí),需要的朋友可以參考下
    2015-06-06
  • 淺談javascript中createElement事件

    淺談javascript中createElement事件

    本文通過示例向大家簡(jiǎn)單介紹了javascript中的createElement事件,需要的朋友可以參考下
    2014-12-12
  • JS backgroundImage控制

    JS backgroundImage控制

    JS backgroundImage控制代碼。
    2009-05-05
  • 最新評(píng)論