JS使用及技巧.

來源:互聯網
上載者:User

標籤:

JS小技巧

  1.如果你JS瞭解的還不深請看 湯姆大叔的部落格 ,肯定讓你上升一個台階。

  2.百科全書 MDN。

  3.兩個非常簡單實用的提示外掛程式 toastr sweetalert。

  4.資料類型的複製:

function copy(arg){    return JSON.parse(JSON.stringify(a));}

  5.數字類型的字串轉數字:

"number" - 0 = number;//"5" - 0 = 5number + "" = "number";//5 + "" = "5"

  6.全能的類型判斷:

function checkType(arg){    return Object.prototype.toString.call(arg).slice(8,-1);}

  7.類數組轉數組:

function likeArray2Array(arg){    return Array.prototype.slice.call(arg);}

  8.判斷對象是否包含某個屬性、遍曆對象的屬性:

"prop" in obj;//判斷對象是否包含屬性//遍曆對象屬性for(var item in obj){    do sth……}

  9.數組filter的妙用:

/**數組去重*/var dirtyArr = [1,2,3,5,3,1,]function clean(arg){    return arg.filter(function(value,index,arr){        return arr.indexOf(value) === index;    })}/**數組過濾*/var originArr = [    {name:"A",age:23},    {name:"B",age:22},    {name:"C",age:26}]var needArr = originArr .filter(function(item){    return item.age>25})

  10.call,apply妙用:

/**取出一個數組的最大值*/var arr = [1,34,3,345,2,44,24,5];Math.max.apply(null,arr);//345Math.min.apply(null,arr);//1

  11.A&&B模式的使用:左邊為true才執行右邊

function _alert(arg){    arg&&alert(arg);}_alert(1);

 

  歡迎補充

JS使用及技巧.

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.