Js基礎資料型別 (Elementary Data Type)常用方法擴充

來源:互聯網
上載者:User

標籤:資料   io   art   for   問題   cti   

1.    Array的contains方法

Array沒有一個contains方法,在現實的應用情境是,有時候我們需要判斷某一個值是否

在該數組中,這個時候一個contains方法就顯得很有必要,具體實現如下:

//判斷數組中是否包含某個元素

Array.prototype.contains = function (obj) {

    var i = this.length;

    while (i--) {

        if (this[i] === obj) {

            return true;

        }

    }

    return false;

}

2.    String的contains方法

同樣的問題也存在於String類型中,在js中同樣也沒有一個用來判斷某一子串是否包

含在母字串中的方法,具體實現如下:

//字串中是否包含某字串

String.prototype.contains = function contains(string, substr, isIgnoreCase) {

    if (isIgnoreCase) {

        this = this.toLowerCase();

        string = string.toLowerCase();

        substr = substr.toLowerCase();

    }

    var startChar = substr.substring(0, 1);

    var strLen = substr.length;

    for (var j = 0; j < string.length - strLen + 1; j++) {

        if (string.charAt(j) == startChar)//

        {

            if (string.substring(j, j + strLen) == substr)//

            {

                return true;

            }

        }

    }

    return false;

3.    Date的addDays、addMonths、addYear、Format方法

熟悉C#的朋友,都會很熟悉也很享受關於DateTime的一系列的便利的操作,在js中並

沒有像C#中那樣便利的有關時間的操作,有時候不免會用到時間的加減等相關的互動,這裡專門對Date類型進行了擴充,具體如下:

//添加天

Date.prototype.addDays = function (d) {

    this.setDate(this.getDate() + d);

};

 

//添加周

Date.prototype.addWeeks = function (w) {

    this.addDays(w * 7);

};

 

//添加月

Date.prototype.addMonths = function (m) {

    var d = this.getDate();

    this.setMonth(this.getMonth() + m);

 

    if (this.getDate() < d)

        this.setDate(0);

};

//添加年

Date.prototype.addYears = function (y) {

    var m = this.getMonth();

    this.setFullYear(this.getFullYear() + y);

 

    if (m < this.getMonth()) {

        this.setDate(0);

    }

};

//日期的格式處理

//日期格式化

Date.prototype.Format = function (fmt) { //author: meizz   

    var o = {

        "M+": this.getMonth() + 1,                 //月份   

        "d+": this.getDate(),                    //日   

        "h+": this.getHours(),                   //小時   

        "m+": this.getMinutes(),                 //分   

        "s+": this.getSeconds(),                 //秒   

        "q+": Math.floor((this.getMonth() + 3) / 3), //季度

        "S": this.getMilliseconds()             //毫秒   

    };

    if (/(y+)/.test(fmt))

        fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));

    for (var k in o)

        if (new RegExp("(" + k + ")").test(fmt))

            fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));

    return fmt;

};

4.    Math.max.apply(null,array),求數組中的最大值

該方法主要用來求一個數組中的最大值,這種情境在實際的工作中也會經常用遇到。或

許會有朋友問到,為什麼不直接調用Math.max()方法?需要注意的是Math.max()方法支援多個參數的傳遞,但是它不支援直接傳遞一個數組作為參數,但是所有的函數都有apply(範圍,參數)這樣的一個方法,我們通過apply方法,間接的將數組作為參數,並且將數組中的每個值拆開來傳遞給了max方法,進而達到了求出最大值的需求。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.