2009-07-14 116 views
9

我们Mootoolers和原型(通常在本站点上)通常携带一个我们创建(或借用)的函数的方便工具箱,我们在原生javascript对象上实现,以使我们的生活变得更加轻松。我希望得到非常有用的原型函数列表,但只有那些在本机对象上实现的列表(例如,mootools中的String.implement({...)。你最喜欢的Mootools/Prototype原生对象原型是什么?

那么,你最喜欢的是什么? PS:我包括mootools和原型作为为一个库编写的函数很容易移植到其他。

PPS:我知道关于原型对原生javascript对象的争论,我宁愿避免这里的讨论。

+5

终于来了!一些同伴MooToolers! – 2009-07-30 15:10:52

回答

1

这里是我最喜欢的一些mootools。

字符串函数

String.implement({ 

    //easy way to test if a string contains characters (input.value.isEmpty()) 
    isEmpty : function() { 
     return (!this.test(/\w+/)); 
    }, 

    //add ellipses if string length > len 
    ellipse : function(len) { 
     return (this.length > len) ? this.substr(0, len) + "..." : this; 
    }, 

    //finds all indexOf occurrences 
    indexesOf : function(val) { 
     var from = 0; 
     var indexes = []; 
     while (0 <= from && from < this.length) { 
      var idx = this.indexOf(val, from); 
      if (idx >= 0) { 
       indexes.push(idx); 
      } else { 
       break; 
      } 
      from = idx+1; 
     } 
     return indexes; 
    } 
}); 

阵列功能

Array.implement({ 

    //compare two arrays to see if they are identical 
    compare : function(arr, strict) { 
     strict = strict || false; 
     if (this.length != arr.length)   return false; 

     for (var i = 0; i < this.length; i++) { 
      if ($type(this[i]) == "array") { 
       if (!this[i].compare(arr[i])) return false; 
      } 
      if (strict) { 
       if (this[i] !== arr[i])  return false; 
      } else { 
       if (this[i] != arr[i])  return false; 
      } 
     } 
     return true; 
    }, 

    //remove non-unique array values 
    unique : function() { 
     for(var i = 0; i< this.length; i++) { 
      var keys = this.indexesOf(this[i]); 
      while (keys.length > 1) { 
       this.splice(keys.pop(), 1); 
      } 
     } 
     return this; 
    }, 

    //same as array.unshift, except returns array instead of count 
    //good for using inline... array.lpush('value').doSomethingElse() 
    lpush : function() { 
     for (var i = arguments.length -1 ; i >= 0; i--){ 
      this.unshift(arguments[i]); 
     } 
     return this; 
    }, 

    //get all indexes of an item in an array 
    indexesOf : function(item) { 
     var ret = []; 
     for (var i = 0; i < this.length; i++) { 
      if (this[i] == item) ret.push(i); 
     } 
     return ret; 
    } 
}); 
1
//taken from http://prototype.lighthouseapp.com/projects/8886/tickets/351-new-swap-method-for-elements 
Element.addMethods({ 
    swap: (function() { 
    if ('swapNode' in document.documentElement) 
     return function(element, other) { 
     return $(element).swapNode($(other)); 
     }; 
    return function(element, other) { 
     element = $(element); 
     other = $(other); 
     var next = other.nextSibling, parent = other.parentNode; 
     element.parentNode.replaceChild(other, element); 
     return parent.insertBefore(element, next); 
    }; 
    })() 
}); 


// extend the array object to support indexed insertions 
// submitted at http://prototype.lighthouseapp.com/projects/8886-prototype/tickets/356-arrayinsert 
Array.prototype.insert=function(element,where) { 
    var slice1=this.slice(0,where); 
    var slice2=this.slice(where); 

    return new Array.concat(slice1,element,slice2); 
}; 


//extend the array object to support searching thrtough indexed arrays 
// if returnIndex is true, then return the keyName, else return the value from that cell 
Array.prototype.nextValue=function(startIndex,returnIndex) { 
    for(var i=startIndex+1;i<this.length;i++){ 
     if(this[i]){ 
      return (returnIndex?i:this[i]); 
     } 
    } 
    return null; 
}; 


//extend the array object to support searching thrtough indexed arrays 
// if returnIndex is true, then return the keyName, else return the value from that cell 
Array.prototype.prevValue=function(startIndex,returnIndex) { 
    for(var i=startIndex-1;i>=0;i--){ 
     if(this[i]){ 
      return (returnIndex?i:this[i]); 
     } 
    } 
    return null; 
}; 
1

我还没有真正得到既没有原型也没有Mootools的发展,但我想下面的东西会在这些框架有用太。

为天然Math.round()代用这需要可选的第二参数,指定的精度:不

Math.round(3.1415, 2); // 3.14 

()为函数来得到一个否定的谓词方法:

var even = function(x){ return x % 2 === 0; }; 
var odd = even.not(); 
even(2); // true 
odd(2); // false 

但是最有用的东西是那些,我会添加到Object.prototype如果这将是一个安全的方式来做到这一点,所以相反,我有一些全局函数来遍历对象属性。

objMap()工作方式类似于Array.map(),但对象:

// returns {a:2, b:4, c:6} 
objMap({a:1, b:2, c:3}, function(value) { 
    return value*2; 
}); 

objValues()objKeys()摆脱对象的属性名称或值的数组:

objValues({a:1, b:2, c:3}); // [1, 2, 3] 
objKeys({a:1, b:2, c:3}); // ["a", "b", "c"] 

当然objReduce()做几乎任何事情想象和...

实施细节已经离开了作为读者的锻炼; Tibial :-)

+0

objMap,objValues和objKeys都在MooTools中实现(通过Hash类)。 – 2010-09-25 00:02:42

1

我喜欢的特性是如何创建前检查,以避免覆盖本机的性能。

if(!Array.prototype.indexOf) { 
    Array.prototype.indexOf = function(){ ... }; 
} 
2

我继续在什么tj111开始,这里是我的小加法:

Array.implement({ 
    //calculate the sum of all integers 
    sum: function() { 
     var sum = this.reduce(function(a, b) { 
      return a + b; 
     }); 
     return sum; 
    } 
}); 
相关问题