在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 教程/ HTML/ typeof 操作符
arguments 對(duì)象
類型轉(zhuǎn)換
構(gòu)造函數(shù)
instanceof 操作符
自動(dòng)分號(hào)插入
為什么不要使用 eval
對(duì)象使用和屬性
作用域與命名空間
this 的工作原理
typeof 操作符
相等與比較
閉包和引用
數(shù)組遍歷與屬性
Array 構(gòu)造函數(shù)
原型
hasOwnProperty 函數(shù)
undefined 和 null
函數(shù)聲明與表達(dá)式
setTimeout 和 setInterval
for in 循環(huán)

typeof 操作符

typeof 操作符(和instanceof一起)或許是 JavaScript 中最大的設(shè)計(jì)缺陷, 因?yàn)閹缀醪豢赡軓乃鼈兡抢锏玫较胍慕Y(jié)果。

盡管 instanceof 還有一些極少數(shù)的應(yīng)用場景,typeof 只有一個(gè)實(shí)際的應(yīng)用(譯者注這個(gè)實(shí)際應(yīng)用是用來檢測一個(gè)對(duì)象是否已經(jīng)定義或者是否已經(jīng)賦值), 而這個(gè)應(yīng)用卻不是用來檢查對(duì)象的類型。

注意: 由于 typeof 也可以像函數(shù)的語法被調(diào)用,比如 typeof(obj),但這并不是一個(gè)函數(shù)調(diào)用。 那兩個(gè)小括號(hào)只是用來計(jì)算一個(gè)表達(dá)式的值,這個(gè)返回值會(huì)作為 typeof 操作符的一個(gè)操作數(shù)。 實(shí)際上不存在名為 typeof 的函數(shù)。

JavaScript 類型表格

    Value               Class      Type
    -------------------------------------
    "foo"               String     string
    new String("foo")   String     object
    1.2                 Number     number
    new Number(1.2)     Number     object
    true                Boolean    boolean
    new Boolean(true)   Boolean    object
    new Date()          Date       object
    new Error()         Error      object
    [1,2,3]             Array      object
    new Array(1, 2, 3)  Array      object
    new Function("")    Function   function
    /abc/g              RegExp     object (function in Nitro/V8)
    new RegExp("meow")  RegExp     object (function in Nitro/V8)
    {}                  Object     object
    new Object()        Object     object

上面表格中,Type 一列表示 typeof 操作符的運(yùn)算結(jié)果??梢钥吹剑@個(gè)值在大多數(shù)情況下都返回 "object"。

Class 一列表示對(duì)象的內(nèi)部屬性 [[Class]] 的值。

JavaScript 標(biāo)準(zhǔn)文檔中定義: [[Class]] 的值只可能是下面字符串中的一個(gè): Arguments, Array, Boolean, Date, Error, Function, JSON, Math, Number, Object, RegExp, String.

為了獲取對(duì)象的 [[Class]],我們需要使用定義在 Object.prototype 上的方法 toString。

對(duì)象的類定義

JavaScript 標(biāo)準(zhǔn)文檔只給出了一種獲取 [[Class]] 值的方法,那就是使用 Object.prototype.toString。

    function is(type, obj) {
        var clas = Object.prototype.toString.call(obj).slice(8, -1);
        return obj !== undefined && obj !== null && clas === type;
    }

    is('String', 'test'); // true
    is('String', new String('test')); // true

上面例子中,Object.prototype.toString 方法被調(diào)用,this 被設(shè)置為了需要獲取 [[Class]] 值的對(duì)象。

譯者注Object.prototype.toString 返回一種標(biāo)準(zhǔn)格式字符串,所以上例可以通過 slice 截取指定位置的字符串,如下所示:

    Object.prototype.toString.call([])  // "[object Array]"
    Object.prototype.toString.call({})  // "[object Object]"
    Object.prototype.toString.call(2)   // "[object Number]"

ES5 提示: 在 ECMAScript 5 中,為了方便,對(duì) nullundefined 調(diào)用 Object.prototype.toString 方法, 其返回值由 Object 變成了 NullUndefined。

譯者注這種變化可以從 IE8 和 Firefox 4 中看出區(qū)別,如下所示:

    // IE8
    Object.prototype.toString.call(null)    // "[object Object]"
    Object.prototype.toString.call(undefined)   // "[object Object]"

    // Firefox 4
    Object.prototype.toString.call(null)    // "[object Null]"
    Object.prototype.toString.call(undefined)   // "[object Undefined]"

測試為定義變量

    typeof foo !== 'undefined'

上面代碼會(huì)檢測 foo 是否已經(jīng)定義;如果沒有定義而直接使用會(huì)導(dǎo)致 ReferenceError 的異常。 這是 typeof 唯一有用的地方。

結(jié)論

為了檢測一個(gè)對(duì)象的類型,強(qiáng)烈推薦使用 Object.prototype.toString 方法; 因?yàn)檫@是唯一一個(gè)可依賴的方式。正如上面表格所示,typeof 的一些返回值在標(biāo)準(zhǔn)文檔中并未定義, 因此不同的引擎實(shí)現(xiàn)可能不同。

除非為了檢測一個(gè)變量是否已經(jīng)定義,我們應(yīng)盡量避免使用 typeof 操作符。