2009-07-14 28 views
9

Chúng tôi Mootoolers và Prototypers (những gì ít trên trang web này) thường mang theo một hộp công cụ tiện dụng của chức năng chúng tôi đã tạo ra (hoặc mượn) mà chúng tôi thực hiện trên các đối tượng javascript bản địa để làm cho cuộc sống của chúng tôi dễ dàng hơn một chút. Tôi muốn có được một danh sách với nhau của các chức năng prototyped rất hữu ích, nhưng chỉ có những người được thực hiện trên các đối tượng bản địa (tức là String.implement({... trong mootools).Nguyên mẫu đối tượng gốc Mootools/Prototype yêu thích của bạn là gì?

Vì vậy, mục yêu thích của bạn là gì?


PS: Tôi đưa cả mootools và nguyên mẫu làm hàm được viết cho một thư viện khá dễ dàng chuyển sang thư viện khác.

PPS: Tôi biết các đối số cho/chống lại việc tạo mẫu đối tượng javascript gốc, tôi muốn tránh thảo luận ở đây.

+5

Cuối cùng! Một số MooToolers đồng nghiệp! –

Trả lời

1

Đây là một số mục yêu thích của tôi cho mootools.

chức năng Chuỗi

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; 
    } 
}); 

chức năng Mảng

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

tôi đã không thực sự được phát triển với không Prototype hay Mootools, nhưng tôi đoán những điều sau đây sẽ có ích trong những khuôn khổ quá.

thay thế cho nguồn gốc Math.round() mà có tham số thứ hai tùy chọn chỉ định chính xác:

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

không() phương pháp cho các chức năng để có được một vị phủ nhận:

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

Nhưng những thứ hữu ích nhất là những thứ mà tôi sẽ thêm vào Object.prototype nếu đó là một cách an toàn để làm điều đó, vì vậy thay vào đó tôi có một số hàm toàn cục để lặp qua đối tượng tính chất.

objMap() hoạt động như Array.map() nhưng đối với các đối tượng:

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

objValues ​​()objKeys() để có được hàng loạt các tên thuộc tính hoặc giá trị từ đối tượng:

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

Và tất nhiên objReduce() để làm hầu hết mọi thứ có thể tưởng tượng ...

chi tiết thi hành đã bị bỏ lại như một excercise cho người đọc :-)

+0

objMap, objValues ​​và objKeys được thực hiện trong MooTools (thông qua lớp Hash). –

1

Tôi thích như thế nào tài sản được kiểm tra trước khi sáng tạo, để tránh ghi đè các thuộc tính bản địa.

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

tôi tiếp tục vào những gì tj111 bắt đầu, đây là bổ sung nhỏ của tôi:

Array.implement({ 
    //calculate the sum of all integers 
    sum: function() { 
     var sum = this.reduce(function(a, b) { 
      return a + b; 
     }); 
     return sum; 
    } 
}); 
Các vấn đề liên quan