typeof

此函数将任何给定变量的数据类型作为字符串返回。

下表列出了可能的返回值:

字符串(String)描述
"number"The variable holds a real (floating point) number - this can include NaN and infinity
"string"The variable holds a string
"array"The variable references an array
"bool"The variable holds a boolean (true / false)
"int32"The variable holds a 32bit integer. This type isn't supported on HTML5.
"int64"The variable holds a 64 bit integer
"ptr"The variable holds a pointer
"undefined"The variable is undefined
"null"The variable holds a null value (this should not be seen normally)
"method"The variable holds a function reference
"struct"The variable holds a struct reference
"ref"The variable holds a handle reference
"unknown"Value is unknown. This should never be seen and signifies that something has gone wrong at the most basic level like a memory overwrite

请注意,在某些情况下,此函数可能无法为 方法 返回正确的值。请考虑在 脚本资产 中编写的以下两个函数定义:

a = function()
{
    // something
}

function b()
{
    // Something
}

从技术上讲,它们都被视为方法,因为它们将函数绑定到变量,但是对函数 b 调用 typeof 将返回“ref”而 不是 “方法”, 在 a 上调用它时将返回“method”。 这是因为为 b 创建的方法被分配了脚本引用( 句柄 ),因为这是编译器识别脚本函数的方式。

 

语法:

typeof(variable);

参数类型描述
variableAny获取的数据类型的变量。

 

返回:

String (see table above)

 

示例:

var _str = typeof(global.ExtensionInput);
show_debug_message(" global.ExtensionInput is a " + _str);

上面的代码获取给定全局变量持有的数据类型,并将字符串返回给本地变量,然后将其用于向控制台输出消息。