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

JS沙箱模式實(shí)例分析

 更新時(shí)間:2017年09月04日 12:06:28   作者:藍(lán)精靈依米  
這篇文章主要介紹了JS沙箱模式,結(jié)合實(shí)例形式分析了JS沙箱模式的原理與實(shí)現(xiàn)方法,需要的朋友可以參考下

本文實(shí)例講述了JS沙箱模式。分享給大家供大家參考,具體如下:

//SandBox(['module1,module2'],function(box){});
/*
*
*
* @function
* @constructor
* @param []  array   模塊名數(shù)組
* @param callback function 回調(diào)函數(shù)
* 功能:新建一塊可用于模塊運(yùn)行的環(huán)境(沙箱),自己的代碼放在回調(diào)函數(shù)里,且不會(huì)對(duì)其他的個(gè)人沙箱造成影響
和js模塊模式配合的天衣無縫
*
* */
function SandBox() {
  //私有的變量
  var args = Array.prototype.slice.call(arguments),
      callback = args.pop(),
      //模塊可以作為一個(gè)數(shù)組傳遞,或作為單獨(dú)的參數(shù)傳遞
      modules = (args && typeof args[0] == "string") ? args : args[0];
  //確保該函數(shù)作為構(gòu)造函數(shù)調(diào)用
  if (!(this instanceof SandBox)) {
    return new SandBox(modules,callback);
  }
  //不指定模塊名和“*”都表示“使用所有模塊”
  if (!modules || modules[0] === "*") {
    for(value in SandBox.modules){
      modules.push(value);
    }
  }
  //初始化所需要的模塊(將想要的模塊方法添加到box對(duì)象上)
    for (var i = 0; i < modules.length; i++) {
      SandBox.modules[modules[i]](this);
    }
  //自己的代碼寫在回調(diào)函數(shù)里,this就是擁有指定模塊功能的box對(duì)象
  callback(this);
}
 SandBox.prototype={
   name:"My Application",
   version:"1.0",
   getName:function() {
     return this.name;
   }
 };
/*
* 預(yù)定義的模塊
*
* */
SandBox.modules={};
SandBox.modules.event=function(box){
  //私有屬性
  var xx="xxx";
  //公共方法
  box.attachEvent=function(){
    console.log("modules:event------API:attachEvent")
  };
  box.dettachEvent=function(){
  };
}
SandBox.modules.ajax=function(box) {
  var xx = "xxx";
  box.makeRequest = function () {
  };
  box.getResponse = function () {
  };
}
SandBox(['event','ajax'],function(box){
  box.attachEvent();
})

運(yùn)行效果截圖:

更多關(guān)于JavaScript相關(guān)內(nèi)容可查看本站專題:《javascript面向?qū)ο笕腴T教程》、《JavaScript中json操作技巧總結(jié)》、《JavaScript切換特效與技巧總結(jié)》、《JavaScript查找算法技巧總結(jié)》、《JavaScript動(dòng)畫特效與技巧匯總》、《JavaScript錯(cuò)誤與調(diào)試技巧總結(jié)》、《JavaScript數(shù)據(jù)結(jié)構(gòu)與算法技巧總結(jié)》、《JavaScript遍歷算法與技巧總結(jié)》及《JavaScript數(shù)學(xué)運(yùn)算用法總結(jié)

希望本文所述對(duì)大家JavaScript程序設(shè)計(jì)有所幫助。

相關(guān)文章

最新評(píng)論