亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

javascript forEach通用循環(huán)遍歷方法

 更新時(shí)間:2010年10月11日 22:22:31   投稿:mdxy-dxy  
循環(huán)遍歷一個(gè)元素是開發(fā)中最常見的需求之一,那么讓我們來看一個(gè)由框架BASE2和Jquery的結(jié)合版本吧.

上一次的錯(cuò)誤太多,排版也出現(xiàn)了問題,重寫了一遍,希望大家支持.

循環(huán)遍歷一個(gè)元素是開發(fā)中最常見的需求之一,那么讓我們來看一個(gè)由框架BASE2和Jquery的結(jié)合版本吧.

var forEach = (function(){
  //數(shù)組與偽數(shù)組的遍歷
  var _Array_forEach = function (array, block, context) { 
    if (array == null) return; 
    //對(duì)String進(jìn)行特殊處理
    if(typeof array == 'string'){
      array = array.split('');
    }
    var i = 0,length = array.length; 
    for (;i < length && block.call(context, array[i], (i+1), array)!==false; i++) {}  
  }; 
  //對(duì)象的遍歷
  var _Function_forEach = function (object, block, context) { 
    for (var key in object) { 
      //只遍歷本地屬性 
      if (object.hasOwnProperty(key)&&block.call(context, object[key], key, object)===false){
        break;
      }
    } 
  }; 
  return function(object, block, context){
    if (object == null) return; 
    if (typeof object.length == "number") { 
     _Array_forEach(object, block, context); 
    }else{  
      _Function_forEach(object, block, context); 
    }
  };
})()

函數(shù)本身并不復(fù)雜,但很精巧。加了一些簡(jiǎn)單的注釋,想信大家能看懂。

來看一點(diǎn)例子

//1:1  \n 2:2 
forEach([1,2,3,4,5],function(el,index){
  if(index>2){
    return false;
  }
  alert(index+":"+el);
});
 
function print(el,index){
  alert(index+":"+el);
}
//a:a  \n b:b   \n c:c
forEach({a:'a',b:'b',c:'c'},print);
 
//1:笨  \n 2:蛋   \n 3:的   \n 4:座   \n 5:右   \n 6:銘
forEach("笨蛋的座右銘",print);
   
function Person(name, age) { 
 this.name = name || ""; 
 this.age = age || 0; 
}; 
Person.prototype = new Person; 
var fred = new Person("笨蛋的座右銘", 25); 
fred.language = "chinese";//極晚綁定 
//name:jxl \n age:22 \n language:chinese
forEach(fred,print);

注:回調(diào)函數(shù)中的index參數(shù)下標(biāo)從1開始

為什么不用內(nèi)置的forEach

和getElementsByClassName一樣,內(nèi)置的forEach效率很高,但是在功能上有局限性,它無法在循環(huán)中途退出。而在我們這個(gè)forEach中,它可以在處理函數(shù)內(nèi)通過返回false的方式退出循環(huán),更加的靈活。

特別的length屬性

length屬性是一個(gè)很特別的屬性,看到數(shù)組,大家一定會(huì)想到length,那看到帶有l(wèi)ength屬性的對(duì)象呢?那大家一定要想到偽數(shù)組(類數(shù)組)。那什么是偽數(shù)組呢?簡(jiǎn)單的理解就是能通過Array.prototype.slice轉(zhuǎn)換為真正的數(shù)組的帶有l(wèi)ength屬性的對(duì)象。javascript最為著名的偽數(shù)組就是arguments對(duì)象。關(guān)于偽數(shù)組有很多東西,以后我會(huì)專門寫一篇博文講這個(gè)東西。大家只要記住:不要隨便給對(duì)象賦予length屬性,除非你明確的知道,你準(zhǔn)備把它當(dāng)作偽數(shù)組來使用。

我想這個(gè)函數(shù)是一個(gè)簡(jiǎn)單javascript工具庫中的必備函數(shù),它是金字塔的根基,在它的基礎(chǔ)上,進(jìn)行再封裝,可以讓你的庫更強(qiáng)大,更加美麗!

以下是補(bǔ)充

在Base2中找到一個(gè)叫forEach的函數(shù),是我見過的最好的實(shí)現(xiàn)。挖出來分析一下。它能對(duì)各種普通對(duì)象,字符串,數(shù)組以及類數(shù)組進(jìn)行遍歷。如果原游覽器的對(duì)象已實(shí)現(xiàn)此函數(shù),它則調(diào)用原對(duì)象的函數(shù)。

function forEach(object, block, context, fn) {
 if (object == null) return;
 if (!fn) {
  if (typeof object == "function" && object.call) {
   //遍歷普通對(duì)象
   fn = Function;
  } else if (typeof object.forEach == "function" && object.forEach != arguments.callee) {
   //如果目標(biāo)已經(jīng)實(shí)現(xiàn)了forEach方法,則使用它自己的forEach方法(如標(biāo)準(zhǔn)游覽器的Array對(duì)象)
   object.forEach(block, context);
   return;
  } else if (typeof object.length == "number") {
   // 如果是類數(shù)組對(duì)象或IE的數(shù)組對(duì)象
   _Array_forEach(object, block, context);
   return;
  }
 }
 _Function_forEach(fn || Object, object, block, context);
};
 
function _Array_forEach(array, block, context) {
 if (array == null) return;
 var i = 0,length = array.length;
 if (typeof array == "string") {
  for (; i < length; i++) {
   block.call(context, array.charAt(i), i, array);
  }
 }else{
  for (;i < length; i++) {
   block.call(context, array[i], i, array);
  }
 }
};
 
 
 _Function_forEach = function(fn, object, block, context) {
  // 這里的fn恒為Function
  for (var key in object) {
    //只遍歷本地屬性
    if (object.hasOwnProperty(key)) {
    //相當(dāng)于 block(object[key], key)
    block.call(context, object[key], key, object);
   }
  }
 };

原作者的一些例子(我FQ扒過來了?。?/p>

function print(el,index){
 alert(index+" : "+el)
}
forEach ([1, 2, 3], print);
forEach ({a: "aa", b: "bb",c: "cc"}, print);
forEach ("司徒正美", print);
forEach(document.styleSheets,function(el){
 if(el.href) alert(el.href)
});

司徒正美的

function forEach(object, block, context, fn) {
   if (object == null) return;
   if (!fn) {
    if (typeof object == "function" && object.call) {
     //遍歷普通對(duì)象
     fn = Function;
    } else if (typeof object.forEach == "function" && object.forEach != arguments.callee) {
     //如果目標(biāo)已經(jīng)實(shí)現(xiàn)了forEach方法,則使用它自己的forEach方法(如標(biāo)準(zhǔn)游覽器的Array對(duì)象)
     object.forEach(block, context);
     return;
    } else if (typeof object.length == "number") {
     // 如果是類數(shù)組對(duì)象或IE的數(shù)組對(duì)象
     _Array_forEach(object, block, context);
     return;
    }
   }
   _Function_forEach(fn || Object, object, block, context);
  };

  function _Array_forEach(array, block, context) {
   if (array == null) return;
   var i = 0,length = array.length;
   if (typeof array == "string")
    array = array.split("");
   for (;i < length; i++) {
    block.call(context, array[i], i, array);
   }
 };


  _Function_forEach = function(fn, object, block, context) {
   // 這里的fn恒為Function
   for (var key in object) {
     //只遍歷本地屬性
     if (object.hasOwnProperty(key)) {
     //相當(dāng)于 block(object[key], key)
     block.call(context, object[key], key, object);
    }
   }
  };
 

 function print(el,index){
  alert(index+" : "+el)
 }
 forEach ([1, 2, 3], print);
 forEach ({a: "aa", b: "bb",c: "cc"}, print);
 forEach ("司徒正美", print);
 forEach(document.styleSheets,function(el){
  if(el.href) alert(el.href)
 });

代碼

function Person(name, age) {
 this.name = name || "";
 this.age = age || 0;
};
 
 
var fred = new Person("Fred", 38);
fred.language = "English";//極晚綁定
fred.wife = "Wilma";//極晚綁定
forEach(fred,print)

完整代碼

 
  function forEach(object, block, context, fn) {
   if (object == null) return;
   if (!fn) {
    if (typeof object == "function" && object.call) {
     //遍歷普通對(duì)象
     fn = Function;
    } else if (typeof object.forEach == "function" && object.forEach != arguments.callee) {
     //如果目標(biāo)已經(jīng)實(shí)現(xiàn)了forEach方法,則使用它自己的forEach方法(如標(biāo)準(zhǔn)游覽器的Array對(duì)象)
     object.forEach(block, context);
     return;
    } else if (typeof object.length == "number") {
     // 如果是類數(shù)組對(duì)象或IE的數(shù)組對(duì)象
     _Array_forEach(object, block, context);
     return;
    }
   }
   _Function_forEach(fn || Object, object, block, context);
  };

  function _Array_forEach(array, block, context) {
   if (array == null) return;
   var i = 0,length = array.length;
   if (typeof array == "string")
    array = array.split("");
   for (;i < length; i++) {
    block.call(context, array[i], i, array);
   }
 };

 
  _Function_forEach = function(fn, object, block, context) {
   // 這里的fn恒為Function
   for (var key in object) {
    //只遍歷本地屬性
     if (object.hasOwnProperty(key)) {
     //相當(dāng)于 block(object[key], key)
     block.call(context, object[key], key, object);
    }
   }
  };
 
 function print(el,index){
  alert(index+" : "+el)
 }
   function Person(name, age) {
   this.name = name || "";
   this.age = age || 0;
  };
 
  
  var fred = new Person("Fred", 38);
  fred.language = "English";//極晚綁定
  fred.wife = "Wilma";//極晚綁定
  forEach(fred,print)
 

在Base2中還提供了一個(gè)叫unbind的方法,我們可以用它把原生對(duì)象的forEach方法剝離出來供我們調(diào)用:

核心代碼

var _slice = Array.prototype.slice;
function unbind(fn) {
 return function(context) {
  return fn.apply(context, _slice.call(arguments, 1));
 };
};

完整代碼

 
try{
 var _slice = Array.prototype.slice;
  function unbind(fn) {
   return function(context) {
    return fn.apply(context, _slice.call(arguments, 1));
   };
  };
  function print(el,index){
   alert(index+" : "+el)
  }
  var each = unbind(Array.prototype["forEach"])
  var a = {cc : function(e){alert(e)}};
  each(["11111","22222"],a.cc,a)//最后的a可有可無
}catch(e){alert("請(qǐng)?jiān)跇?biāo)準(zhǔn)瀏覽器中使用!")} 

這篇文章就介紹到這了,希望大家以后多多支持腳本之家。

相關(guān)文章

最新評(píng)論