2013-03-22 84 views
2

我試圖jQuery的擴展轉換下面爲原型形延伸:jQuery的擴展原型轉換

$.some = function(array, cmp_bool, context) { 
    if (Array.prototype.some) { 
     return array.some(cmp_bool, context); 
    } else { 
     if (context) { 
      cmp_bool = $.proxy(cmp_bool, context); 
     } 
     return !!($.grep(array, cmp_bool).length) 
    } 
}; 

回答

1

PrototypeJS已經本已內置於核心。

數組對象類型中的可枚舉的方法混合 - 其具有some()方法完全相同的參數(不陣列作爲第一個參數作爲您正在作用於Array實例)

所以給出的這些

var testit = function(t){ 
    return t < 10; 
} 
var myArray = [1, 2, 3, 7, 10]; 

您所提供的jQuery的擴展調用這樣

$.some(myArray,testit); 
//or noConflict() mode 
jQuery.some(myArray,testit); 

和內置的原型JS方法被調用,這樣

myArray.some(testit); 

**可枚舉的方法some()化名爲被鏈接在這裏的any()方法 http://api.prototypejs.org/language/Enumerable/prototype/any/

+0

有誰知道的jQuery的方法$ .proxy和$ .grep相當於在PrototypeJS中是? – Chris 2013-03-24 13:16:24

+0

$ .proxy()與bind()相同http://api.prototypejs.org/language/Function/prototype/bind/ – 2013-03-24 21:43:10

+0

$ .grep與'findAll()'http:/ /api.prototypejs.org/language/Enumerable/prototype/findAll/ - 如果您反轉了「reject()」的jQuery $ .grep,那麼http://api.prototypejs.org/language/Enumerable/prototype/reject/ – 2013-03-24 21:46:37