Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //function extensions
- Function.prototype.addMethod = function(name, func){
- if(!this.prototype[name]){
- this.prototype[name] = func;
- }
- return this;
- };
- //number extensions
- Number.addMethod('toInteger', function(){
- return Math[this < 0 ? 'ceil' : 'floor'](this);
- });
- //string extensions
- String.addMethod('trim', function(){
- return this.replace(/^\s+|\s+$/g, '');
- });
- String.addMethod('insertAt', function(loc, strChunk){
- var val = this.valueOf().substr(0, loc);
- return val + strChunk + this.valueOf().substr(loc);
- });
- //array extensions
- Array.addMethod('toSet', function(){
- for(var i=0; i < this.length; i++){
- var val = this[i];
- for(var j=i+1; j < this.length; j++)
- if(this[j]==val)
- this.splice(j--, 1);
- }
- return this;
- });
- //first class functions
- var range = function(start, end, step){
- if(step === undefined){
- var step = 1;
- }
- var nums = [];
- while(a != b){
- nums.push(a);
- a += step;
- }
- return nums;
- };
Add Comment
Please, Sign In to add comment