2012-11-01 23 views
1

我認爲JavaScript有5種基本類型(null,undefined,boolean,number,string),然後是對象(包含數組,函數和自定義僞類對象)。但是,有些奇怪的是,ECMA-5是否有新的操作符(或函數)?

typeof null 

"object",並沒有簡單的方法來找回對象類名的僞古典文學課,如PersonAuthor。我想知道是否有一個更新的操作符或函數可以返回可能的小寫名稱爲原始類型(並且"null"null,而不是"object"),並且自定義僞古典對象的大寫字母?

如果在ECMA-5或更高版本中沒有這樣的操作符或函數存在,它有意義嗎?否則,我們可能需要依賴我們自己的定義或任何框架,但這不會在不同的平臺上標準化。

+0

沒有,有沒有新的運營商。他們考慮修復ES6的'typeof'運算符,以便它會返回'「null」'而不是''object'',但是因爲它會打破太多的代碼而退縮。 –

+0

不知道如果可以添加一個新的操作符,例如調用它'pseudoClassOf'或'typeOrClassOf',那麼它可能是有意義的 –

+0

添加新操作符的麻煩是任何代碼都有一個變量,相同的名字會突然中斷。由於JS被廣泛使用,並且由於仍有許多舊代碼仍在使用中,所以他們對可能會破壞腳本的更改非常敏感。因此,他們試圖堅持當前的保留字。 –

回答

3

的ECMAScript 5對象具有被稱爲[[Class]]內部屬性。這是ES5中最接近你要求的東西。您可以通過訪問Object.prototype.toString[[Class]]如下:

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

getClassOf([ ]); // => "Array" 
getClassOf(new Date()); // => "Date" 
getClassOf(function() { }); // => "Function" 
getClassOf(3); // => "Number" 
getClassOf(true) // => "Boolean" 
getClassOf(document.createElement('div')); // => "HTMLDivElement" 
getClassOf(Math); // => "Math" 
getClassOf(null); // => "Null" 
getClassOf(undefined); // => "Undefined" 
getClassOf({ x: 1 }); // => "Object" 

此行爲是充分確定哪些來自其他幀中的對象至關重要。

但是,它不適用於用戶定義的構造函數。使用用戶定義的構造函數創建的對象具有[[Class]]"Object"

function Foo() { } 
var foo = new Foo(); 

getClassOf(foo); // => "Object" 

它看起來像ECMAScript的6可能要延長是怎麼通過Object.prototype.toString返回,這樣getClassOf(foo)可能是"Foo"通過@@toStringTag符號的能力。

有關即將推出的標準的更多信息,請參閱https://mail.mozilla.org/pipermail/es-discuss/2012-September/025344.html


你可以創建自己的功能,做你想做的是這樣的:

function getTypeOf(value) { 

    // Return "null" for null. 
    if (value === null) return 'null'; 

    // Return primitive types. 
    var type = typeof value; 
    if (type != 'object') return type; 

    // Return [[Class]] if available for objects. 
    type = Object.prototype.toString.call(value).slice(8, -1); 
    if (type != 'Object') return type; 

    // Return "Object" if it wasn't created with another constructor. 
    var proto = Object.getPrototypeOf(value); 
    if (proto == Object.prototype) 
     return 'Object'; 

    // Return the constructor name if constructor hasn't been 
    // modified on the object. 
    if (value.constructor && proto === value.constructor.prototype) 
     return value.constructor.name; 

    // Return the constructor name if constructor hasn't been 
    // modified on the prototype. 
    if (proto.constructor && proto === proto.constructor.prototype) 
     return proto.constructor.name; 

    // Return "???" if the type is indeterminable. 
    return '???'; 

} 

例子:

getTypeOf([ ]); // => "Array" 
getTypeOf(new Date()); // => "Date" 
getTypeOf(function() { }); // => "Function" 
getTypeOf(3); // => "number" 
getTypeOf(true) // => "boolean" 
getTypeOf(document.createElement('div')); // => "HTMLDivElement" 
getTypeOf(Math); // => "Math" 
getTypeOf(null); // => "null" 
getTypeOf(undefined); // => "undefined" 
getTypeOf({ x: 1 }); // => "Object" 

function Foo() { } 
var foo = new Foo(); 

getTypeOf(foo); // => "Foo" 

// If the constructor property is changed, still works. 
foo.constructor = function FakeConstructor() { }; 
getTypeOf(foo); // => "Foo" 

// If the constructor property AND the prototype's constructor is 
// changed, result is "???". 
foo.constructor = function FakeConstructor() { }; 
Foo.prototype.constructor = function FakeConstructor2() { }; 
getTypeOf(foo); // => "???" 
0

您可以在所有瀏覽器中立即執行此操作。

任何對象的constructor屬性將返回擁有該對象原型的函數。 - typeof運算結果

Type of val      Result 
----------------------------------------------------------------------------------- 
Undefined      "undefined" 
----------------------------------------------------------------------------------- 
Null        "object" 
----------------------------------------------------------------------------------- 
Boolean       "boolean" 
----------------------------------------------------------------------------------- 
Number       "number" 
----------------------------------------------------------------------------------- 
String       "string" 
----------------------------------------------------------------------------------- 
Object (native and does not 
implement [[Call]])    "object" 
----------------------------------------------------------------------------------- 
Object (native or host 
and does implement [[Call]])  "function" 
----------------------------------------------------------------------------------- 
Object (host and does not  Implementation-defined except may not be 
implement [[Call]])    "undefined", "boolean", "number", or "string". 

(大寫Null

表20:

function Person() { } 
alert(new Person().constructor === Person) //true 
+1

但它依賴於'constructor'屬性被正確設置,這可能並非總是如此,它不會在'null'或'undefined'工作 –

+0

它怎麼可能在未定義?這甚至沒有道理......對於零(大部分時間)都沒有意義。 – jahroy

+1

並沒有什麼能夠阻止某人覆蓋'constructor'屬性! – phenomnomnominal

1

typeof即行爲是由本說明書中,section 11.4.3,表20規定內部類型,其唯一值爲null。)

這似乎只是一個慣例; null instanceof Objectfalse,所以,如預期的那樣,null顯然不是一個對象。

您要求的操作符不存在。要找出名稱,可以使用=== null爲其他基元測試nulltypeof。至於 「自定義的僞古典對象」,在您的處置的工具有:

(列表從this question,在這裏可以看到一些例子。)

相關問題