《JS里的數(shù)據(jù)類(lèi)型》

JS里的數(shù)據(jù)類(lèi)型分為 原始數(shù)據(jù)類(lèi)型(primitive type) 和 合成數(shù)據(jù)類(lèi)型(complex type)。

一、原始數(shù)據(jù)類(lèi)型(primitive type)

   1.數(shù)值(number)

      整數(shù)和小數(shù)(比如1和3.14)
      科學(xué)記數(shù)法(1.23e2)
      二進(jìn)制(0b11)
      八進(jìn)制(011)
      十六進(jìn)制(0x11)

  2.字符串(String)

      空字符串: ' '
      多行字符串:
      var s = '12345' +
          '67890' // 無(wú)回車(chē)符號(hào)
      或
      var s = `12345
      67890` // 含回車(chē)符號(hào)

  3.布爾值(Boolean)

      表示真?zhèn)蔚膬蓚€(gè)值,即true(真)和false(假)
      a && b 在 a 和 b 都為 true 時(shí),取值為 true;否則為 false
      a || b 在 a 和 b 都為 false 時(shí),取值為 false;否則為 true

  4.符號(hào)(Symbol)

    Symbol 生成一個(gè)全局唯一的值。

  5.null
     表示空值,即此處的值為空。

  6.undefined
    表示“未定義”或不存在,即由于目前沒(méi)有定義,所以此處暫時(shí)沒(méi)有任何值

二、合成數(shù)據(jù)類(lèi)型

  對(duì)象(Object)

  各種值組成的集合。在計(jì)算機(jī)科學(xué)中, 對(duì)象是指內(nèi)存中的可以被標(biāo)識(shí)符引用的一塊區(qū)域.
  
  對(duì)象是最復(fù)雜的數(shù)據(jù)類(lèi)型,又可以分成三個(gè)子類(lèi)型:

  狹義的對(duì)象(object)
  數(shù)組(array)
  函數(shù)(function)

三、typeof操作符

  typeof操作符返回一個(gè)字符串,表示未經(jīng)計(jì)算的操作數(shù)的類(lèi)型。

  typeof可能的返回值:
  
   // Numbers
  typeof 37 === 'number';
  typeof 3.14 === 'number';
  typeof Math.LN2 === 'number';
  typeof Infinity === 'number';
  typeof NaN === 'number'; // 盡管NaN是"Not-A-Number"的縮寫(xiě)
  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';

  // null
  typeof null === 'object'; 

  // Undefined
  typeof undefined === 'undefined';
  typeof declaredButUndefinedVariable === 'undefined';
  typeof undeclaredVariable === 'undefined'; 

  // Objects
  typeof {a:1} === 'object';

  // 使用Array.isArray 或者 Object.prototype.toString.call
  // 區(qū)分?jǐn)?shù)組,普通對(duì)象
  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 class C{} === 'function'
  typeof Math.sin === 'function';
  typeof new Function() === 'function';
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

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