»
EnglishFrenchVietnamese

Print - Some Essential JavaScript Prototype Functions to Enhance your JavaScript Applications - JavaScriptBank.com

Full version: jsB@nk » Snippet » Some Essential JavaScript Prototype Functions to Enhance your JavaScript Applications
URL: http://www.javascriptbank.com/some-essential-javascript-prototype-functions-to-enhance-your-javascript-applications.html

Some Essential JavaScript Prototype Functions to Enhance your JavaScript Applications © JavaScriptBank.comThe 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 FunctionThis 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

Full version: jsB@nk » Snippet » Some Essential JavaScript Prototype Functions to Enhance your JavaScript Applications
URL: http://www.javascriptbank.com/some-essential-javascript-prototype-functions-to-enhance-your-javascript-applications.html



JavaScript
<script type="text/javascript">// -- Standard functions// String.contains() - See if a string contains a stringString.prototype.contains = function(str) {    return (this.indexOf(str) != -1);};// String.getChar() - Gets the character at a specific positionString.prototype.getChar = function(char_pos) {    return this.substring(char_pos, char_pos + 1);}// String.trim() - Strips spaces from beginning and end of stringString.prototype.trim = function() {    return this.replace(/^\s+|\s+$/g, '');};// Array.contains() - See if an array contains an objectif (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 arrayif (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 elementArray.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 valueArray.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 keysArray.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 valueArray.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 rangeArray.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 elementsArray.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 valuesArray.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 functionArray.prototype.walk = function(f) {    var a = [],        i = this.length;    while (i--) {        a.push(f(this[i]));    }    return a.reverse();};</script>