»
Tiếng AnhTiếng PhápTiếng Việt

In - Các hàm JavaScript thiết yếu để tăng lực cho ứng dụng web - JavaScriptBank.com

Phiên bản đầy đủ: jsB@nk » Thủ thuật » Các hàm JavaScript thiết yếu để tăng lực cho ứng dụng web
URL: https://www.javascriptbank.com/some-essential-javascript-prototype-functions-to-enhance-your-javascript-applications.html

Các hàm JavaScript thiết yếu để tăng lực cho ứng dụng web © JavaScriptBank.comBài viết này được jsB@nk tổng hợp từ các nguồn khác nhau trên Internet để có được danh sách 13 hàm JavaScript dạng Prototype cực kì hữu ích và thiết yếu để tăng lực cho các ứng dụng JavaScript, ứng dụng web của bạn. Nếu chưa rõ về JavaScript Prototype, bạn vui lòng tìm hiểu rõ hơn khía cạnh rất độc đáo này của ngôn ngữ lập trình JavaScript thông qua các bài viết: - JavaScript Prototype: Some Basic Concepts - Prototype Dollar FunctionBài viết này bao gồm 13 hàm prototype dùng cho dữ liệu JavaScript kiểu mảng, kiểu chuỗi và kiểu số; chi tiết bạn vui lòng vào trang trong, jsB@nk chỉ giới thiệu một vài hàm rất phổ biến như sau: - contains: kiểm tra chuỗi con trong một chuỗi - getChar: lấy kí tự trong chuỗi tại ví trí xác định - trim: cắt bỏ các kí tự rỗng dư thừa ở đầu và cuối chuỗi - copy: sao chép một mảng dữ liệu - random: trả về một dữ liệu ngẫu nhiên trong mảng

Phiên bản đầy đủ: jsB@nk » Thủ thuật » Các hàm JavaScript thiết yếu để tăng lực cho ứng dụng web
URL: https://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>