Some Essential JavaScript Prototype Functions to Enhance your JavaScript Applications

The JavaScript prototype functions in this JavaScript article tutorial are collected by jsB@nk from many JavaScript resources on the Internet. These JavaScript prototype functions are very helpful and essential to your JavaScript applications, web-based applications if you need to reduce time and work to process JavaScript tasks. And if you are still not good at JavaScript prototype functions, please read the JavaScript article below:
- JavaScript Prototype: Some Basic Concepts
- Prototype Dollar Function

This JavaScript article tutorial provides you the JavaScript source codes of 13 JavaScript prototype functions to process JavaScript Strings, JavaScript Arrays and Number variables, please go to the full-post page for details, now jsB@nk shows you some very popular JavaScript prototype functions only:
- contains: check if a string contains a child string
- trim: strips all redundant spaces from beginning and end of string
- copy: copy/clone an array variable
- random: return a random element, optionally up to or from range


Sampled by © JavaScriptBank.com
// -- Standard functions
// String.contains() - See if a string contains a string
String.prototype.contains = function(str) {
    return (this.indexOf(str) != -1);
};

// String.getChar() - Gets the character at a specific position
String.prototype.getChar = function(char_pos) {
    return this.substring(char_pos, char_pos + 1);
}

// String.trim() - Strips spaces from beginning and end of string
String.prototype.trim = function() {
    return this.replace(/^\s+|\s+$/g, '');
};

// Array.contains() - See if an array contains an object
if (typeof Array.prototype.contains === 'undefined') {
    Array.prototype.contains = function(obj) {
        var i = this.length;
        while (i--) {
            if (this[i] === obj) {
                return true;
            }
        }
        return false;
    };
}

// Array.copy() - Copy an array
if (typeof Array.prototype.copy === 'undefined') {
    Array.prototype.copy = function() {
        var a = [],
            i = this.length;
        while (i--) {
            a[i] = typeof this[i].copy !== 'undefined' ? this[i].copy() : this[i];
        }
        return a;
    };
}

// Array.forEach( function ) - Apply a function to each element
Array.prototype.forEach = function(f) {
    var i = this.length,
        j, l = this.length;
    for (i = 0; i < l; i++) {
        if ((j = this[i])) {
            f(j);
        }
    }
};

// Array.indexOf( value, begin, strict ) - Return index of the first element that matches value
Array.prototype.indexOf = function(v, b, s) {
    for (var i = +b || 0, l = this.length; i < l; i++) {
        if (this[i] === v || s && this[i] == v) {
            return i;
        }
    }
    return -1;
};

// Array.insert( index, value ) - Insert value at index, without overwriting existing keys
Array.prototype.insert = function(i, v) {
    if (i >= 0) {
        var a = this.slice(),
            b = a.splice(i);
        a[i] = v;
        return a.concat(b);
    }
};

// Array.lastIndexOf( value, begin, strict ) - Return index of the last element that matches value
Array.prototype.lastIndexOf = function(v, b, s) {
    b = +b || 0;
    var i = this.length;
    while (i-- > b) {
        if (this[i] === v || s && this[i] == v) {
            return i;
        }
    }
    return -1;
};

// Array.random( range ) - Return a random element, optionally up to or from range
Array.prototype.random = function(r) {
    var i = 0,
        l = this.length;
    if (!r) {
        r = this.length;
    }
    else if (r > 0) {
        r = r % l;
    }
    else {
        i = r;
        r = l + r % l;
    }
    return this[Math.floor(r * Math.random() - i)];
};

// Array.shuffle( deep ) - Randomly interchange elements
Array.prototype.shuffle = function(b) {
    var i = this.length,
        j, t;
    while (i) {
        j = Math.floor((i--) * Math.random());
        t = b && typeof this[i].shuffle !== 'undefined' ? this[i].shuffle() : this[i];
        this[i] = this[j];
        this[j] = t;
    }
    return this;
};

// Array.unique( strict ) - Remove duplicate values
Array.prototype.unique = function(b) {
    var a = [],
        i, l = this.length;
    for (i = 0; i < l; i++) {
        if (a.indexOf(this[i], 0, b) < 0) {
            a.push(this[i]);
        }
    }
    return a;
};

// Array.walk() - Change each value according to a callback function
Array.prototype.walk = function(f) {
    var a = [],
        i = this.length;
    while (i--) {
        a.push(f(this[i]));
    }
    return a.reverse();
};

Language
Translate this page to English Translate this page to French Translate this page to Vietnamese

Recent articles
Vue.js developers: hire them, use them and get ahead of the competition
3 Reasons Why Java is so Popular
Migrate to Angular: why and how you should do it
The Possible Working Methods of Python Ideology
JavaScript Research Paper: 6 Writing Tips to Craft a Masterpiece
Learning How to Make Use of New Marketing Trends
5 Important Elements of an E-commerce Website
How To Create A Successful Prototype For Your PCB
WordPress Landing Page Conversion Essentials to Convert More
Lifeasapa and Neurosphere


Top view articles
Top 10 Beautiful Christmas Countdown Timers
65 Free JavaScript Photo Gallery Solutions
Best Free Linux Web Programming Editors
Top 50 Most Addictive and Popular Facebook mini games
Top 10 Free Web Chat box Plug-ins and Add-ons
Top 10 Best JavaScript eBooks that Beginners should Learn
50+ Beautiful Resources of HTML Form using CSS and JavaScript
The Ultimate JavaScript Tutorial in Web Design
10 Great JavaScript Frameworks for Faster JavaScript Application Development
HTML5 Web Workers Multithreading in JavaScript


Free JavaScript Tutorials & Articles
at www.JavaScriptBank.com