2009-07-14 6 views
9

Us Moootooler und Prototypers (die wenigen auf dieser Site) enthalten normalerweise eine handliche Toolbox von Funktionen, die wir erstellt (oder geliehen) haben, die wir auf nativen Javascript-Objekten implementieren, um unser Leben ein wenig einfacher zu machen. Ich wollte eine Liste mit sehr hilfreichen Prototyp-Funktionen erhalten, aber nur solche, die auf nativen Objekten implementiert sind (zB String.implement({... in mootools).Was sind Ihre bevorzugten Prototypen von Mootools/Prototype-Objekten?

Also, was sind deine Favoriten?

PS: Ich habe sowohl Mootools und Prototyp als eine Funktion für eine Bibliothek geschrieben geschrieben ist ziemlich leicht auf die andere portiert.

PPS: Ich kenne die Argumente für/gegen das Prototyping nativer Javascript-Objekte, ich würde diese Diskussion lieber hier vermeiden.

+5

Endlich! Ein paar MooToolers! –

Antwort

1

Hier sind einige meiner Favoriten für Mootools.

String-Funktionen

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-Funktionen

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

Ich habe weder mit Prototype noch Mootools nicht wirklich entwickelt, aber ich denke, die folgenden Dinge in diesen Rahmenbedingungen sinnvoll wäre auch.

Ersatz für native Math.round() die optionalen zweiten Parameter nimmt, die die Genauigkeit angibt:

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

nicht() Methode für Funktionen ein negierte Prädikat zu erhalten:

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

Aber die meisten nützlichen Dinge sind diejenigen, die ich Object.prototype hinzufügen würde, wenn das eine sichere Methode wäre, dies zu tun, stattdessen habe ich einige globale Funktionen, um über Objekt zu iterieren Eigenschaften.

objMap(), die wie Array.map(), aber für Objekte funktioniert:

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

objValues ​​() und objKeys() Array von Eigenschaftsnamen oder Werte von Objekt zu erhalten:

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

Und natürlich objReduce() vorstellbar, fast alles zu tun ...

Implementierungsdetails wurden :-) für den Leser als Übungs links

+0

objMap, objValues ​​und objKeys sind alle in MooTools implementiert (via Hash-Klasse). –

1

Ich mag, wie das Eigentum vor der Schöpfung überprüft wird, native Eigenschaften Überschreiben zu vermeiden.

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

ich auf welchem ​​fortgesetzt tj111 begonnen, hier ist meine kleine Ergänzung:

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