typeof new Function()总是“;函数”;

typeof new Function() always "function"?

本文关键字:函数 总是 new Function typeof      更新时间:2023-09-26

typeof new Number(1)将返回"object",与实例化字符串等相同。

使用typeof myVar === "function"安全吗?或者有没有浏览器在使用Function构造函数时会返回"object"(不知道为什么要这样做,但可能有任何一个)?

您可以使用

if (myVar instanceof Function) 

相反。然而,myVar-instanceofObject也将为true,因为它是派生类型。我不确定这是否"更"兼容跨浏览器,但我相当确定它是ecma标准的一部分。

我在所有应用程序中都使用它,从未遇到过问题。它使用起来很安全。

ECMAScript定义