久久久久久久视色,久久电影免费精品,中文亚洲欧美乱码在线观看,在线免费播放AV片

<center id="vfaef"><input id="vfaef"><table id="vfaef"></table></input></center>

    <p id="vfaef"><kbd id="vfaef"></kbd></p>

    
    
    <pre id="vfaef"><u id="vfaef"></u></pre>

      <thead id="vfaef"><input id="vfaef"></input></thead>

    1. 站長(zhǎng)資訊網(wǎng)
      最全最豐富的資訊網(wǎng)站

      javascript的typeof可返回哪些數(shù)據(jù)類型

      在javascript中,typeof操作符可返回的數(shù)據(jù)類型有:“undefined”、“object”、“boolean”、“number”、“string”、“symbol”、“function”等。

      javascript的typeof可返回哪些數(shù)據(jù)類型

      本教程操作環(huán)境:windows7系統(tǒng)、javascript1.8.5版、Dell G3電腦。

      使用 typeof 操作符可以返回變量的數(shù)據(jù)類型。

      我們來看看各個(gè)數(shù)據(jù)類型對(duì)應(yīng)typeof的值:

      數(shù)據(jù)類型 結(jié)果
      Undefined “undefined”
      Null “object”
      布爾值 “boolean”
      數(shù)值 “number”
      字符串 “string”
      Symbol (ECMAScript 6 新增) “symbol”
      宿主對(duì)象(JS環(huán)境提供的,比如瀏覽器) Implementation-dependent
      函數(shù)對(duì)象 “function”
      任何其他對(duì)象 “object”

      再看看具體的實(shí)例:

      // Numbers typeof 37 === 'number'; typeof 3.14 === 'number'; typeof Math.LN2 === 'number'; typeof Infinity === 'number'; typeof NaN === 'number'; // 盡管NaN是"Not-A-Number"的縮寫,意思是"不是一個(gè)數(shù)字" typeof Number(1) === 'number'; // 不要這樣使用!  // Strings typeof "" === 'string'; typeof "bla" === 'string'; typeof (typeof 1) === 'string'; // typeof返回的肯定是一個(gè)字符串 typeof String("abc") === 'string'; // 不要這樣使用!  // Booleans typeof true === 'boolean'; typeof false === 'boolean'; typeof Boolean(true) === 'boolean'; // 不要這樣使用!  // Symbols typeof Symbol() === 'symbol'; typeof Symbol('foo') === 'symbol'; typeof Symbol.iterator === 'symbol';  // Undefined typeof undefined === 'undefined'; typeof blabla === 'undefined'; // 一個(gè)未定義的變量,或者一個(gè)定義了卻未賦初值的變量  // Objects typeof {a:1} === 'object';  // 使用Array.isArray或者Object.prototype.toString.call方法可以從基本的對(duì)象中區(qū)分出數(shù)組類型 typeof [1, 2, 4] === 'object';  typeof new Date() === 'object';  // 下面的容易令人迷惑,不要這樣使用! typeof new Boolean(true) === 'object'; typeof new Number(1) ==== 'object'; typeof new String("abc") === 'object';  // 函數(shù) typeof function(){} === 'function'; typeof Math.sin === 'function';

      我們會(huì)發(fā)現(xiàn)一個(gè)問題,就是typeof來判斷數(shù)據(jù)類型其實(shí)并不準(zhǔn)確。比如數(shù)組、正則、日期、對(duì)象的typeof返回值都是object,這就會(huì)造成一些誤差。

      所以在typeof判斷類型的基礎(chǔ)上,我們還需要利用Object.prototype.toString方法來進(jìn)一步判斷數(shù)據(jù)類型。

      我們來看看在相同數(shù)據(jù)類型的情況下,toString方法和typeof方法返回值的區(qū)別:

      數(shù)據(jù) toString typeof
      “foo” String string
      new String(“foo”) String object
      new Number(1.2) Number object
      true Boolean boolean
      new Boolean(true) Boolean object
      new Date() Date object
      new Error() Error object
      new Array(1, 2, 3) Array object
      /abc/g RegExp object
      new RegExp(“meow”) RegExp object

      可以看到利用toString方法可以正確區(qū)分出Array、Error、RegExp、Date等類型。

      所以我們一般通過該方法來進(jìn)行數(shù)據(jù)類型的驗(yàn)證。

      贊(0)
      分享到: 更多 (0)
      網(wǎng)站地圖   滬ICP備18035694號(hào)-2    滬公網(wǎng)安備31011702889846號(hào)