2009-07-14 16 views
9

हमें म्यूटूलर्स और प्रोटोटाइप (इस साइट पर कुछ क्या हैं) आमतौर पर हमारे द्वारा बनाए गए कार्यों (या उधार) के एक आसान टूलबॉक्स को ले जाते हैं जिन्हें हम अपने जीवन को थोड़ा आसान बनाने के लिए देशी जावास्क्रिप्ट ऑब्जेक्ट्स पर लागू करते हैं। मैं बहुत उपयोगी प्रोटोटाइप कार्यों के साथ एक सूची प्राप्त करना चाहता था, लेकिन मूल वस्तुओं पर लागू किए गए केवल उन्हीं (यानी String.implement({... mootools में)।आपके पसंदीदा Mootools/प्रोटोटाइप देशी ऑब्जेक्ट प्रोटोटाइप क्या हैं?

तो, आपके पसंदीदा क्या हैं?


पीएस: मैंने एक पुस्तकालय के लिए लिखे गए एक समारोह के रूप में दोनों mootools और प्रोटोटाइप को आसानी से दूसरे को पोर्ट किया है।

पीपीएस: मुझे देशी जावास्क्रिप्ट ऑब्जेक्ट्स प्रोटोटाइप करने के लिए/तर्क के बारे में तर्क पता है, मैं यहां उस चर्चा से बचना पसंद करूंगा।

+5

अंत में! कुछ साथी MooToolers! –

उत्तर

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(3.1415, 2); // 3.14 

नहीं() फ़ंक्शन एक नकार विधेय पाने के लिए विधि:

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

लेकिन सबसे उपयोगी चीजें हैं, जो मैं ऑब्जेक्ट.प्रोटोटाइप में जोड़ दूंगा अगर यह करने का एक सुरक्षित तरीका होगा, इसलिए इसके बजाय मेरे पास ऑब्जेक्ट पर पुन: सक्रिय करने के लिए कुछ वैश्विक कार्य हैं गुण।

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() लगभग कल्पना कुछ भी करने को की ...

क्रियान्वयन विवरण :-)

+0

objMap, objValues ​​और objKeys सभी MooTools (हैश क्लास के माध्यम से) में लागू किए गए हैं। –

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; 
    } 
}); 
संबंधित मुद्दे