2015-02-17 23 views
0

我可以在JavaScript中識別以下幾種類型:在JavaScript中函數是一個離散類型嗎?

number, string, boolean, object, null and undefined 

但是,是function離散型,或者是它的object一個實例?

+4

函數是'Function'構造函數的實例。它們是具有特殊運行時支持特性的對象(如函數)。 – Pointy 2015-02-17 22:46:45

+0

'typeof'也認爲'null'是'Object'類型,'undefined'實際上不是一個類型。 – Pointy 2015-02-17 22:47:27

+0

因此'typeof'的返回值對應於語言中類型的超集? – Ben 2015-02-17 22:53:26

回答

1

javascript中的函數是從Object繼承的。您可以嘗試以下操作:

var my_func = function(){}; 
console.log(my_func instanceof Object); // prints true 
console.log(my_func instanceof Function); // also prints true 

Array的情況也是如此。

相關問題