JavaScript概覽

  • 基礎(chǔ)類型:number、boolean、string、null、undefined

  • 復(fù)雜類型:array、function、object

  • typeof

   typeof 0 ==> "number"
   typeof false ==> "boolean"
   typeof "" ==> "string"
   typeof null ==> "object"
   typeof undefined ==> "undefined"
   typeof [] ==> "object"
   typeof function(){} ==> "function"
   typeof {} ==> "object"
   
   typeof new Number(0) ==> "object"
   typeof new Boolean(false) ==> "object"
   typeof new String("") ==> "object"
   typeof new Array() ==> "object"
   typeof new Function() ==> "function"
   typeof new Object() ==> "object"
  • instanceof
    object instanceof constructor
    如果constructor.prototype.isPrototypeOf(object),則返回true

  • call&apply
    call和apply都可以改變this的值;
    call接受參數(shù)列表,apply接受參數(shù)數(shù)組。

   function a(b, c) {
       b == "first";
       c == "second";
   }
   
   a.call({a: "b"}, "first", "second");
   a.apply({a: "b"}, ["first", "second"]);
  • try-catch
    使用throw拋出錯誤時,代碼停止往下執(zhí)行;
    可以使用try-catch對異常進行捕獲,使代碼可以繼續(xù)執(zhí)行下去。

  • 遍歷object的keys

 for (var i in a) {
       if (a.hasOwnProperty(i)) {
       }
   }

或者在V8引擎中:
Object.keys(a);

  • 判斷一個對象是數(shù)組
    Array.isArray();

  • 數(shù)組方法

遍歷數(shù)組

   [1, 2, 3].forEach(function (v) {
       console.log(v);
   });

過濾數(shù)組

   [1, 2, 3].filter(function (v) {
       return v < 3;
   }); ==> [1, 2]

改變數(shù)組

   [1, 2, 3].map(function (v) {
       return v * 2;
   }); ==> [2, 4, 6]
  • 字符串方法

移除空格
" hello ".trim(); ==> "hello"

  • bind
    function a() {
        this.hello == "world";
    }
    
    var b = a.bind({ hello: "world"});
    b();  ==> true
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容