Skip to content

Latest commit

 

History

History
33 lines (21 loc) · 964 Bytes

get-type.md

File metadata and controls

33 lines (21 loc) · 964 Bytes
标题 标签
getType(获取数据类型) type(数据类型)

返回值的数据类型。

  • 如果值为 undefined 或 null,则返回 'undefined' 或 'null'。
  • 否则,使用 Object.prototype.constructor 和 Function.prototype.name 来获取构造函数的名称。
const getType = v =>
  v === undefined ? 'undefined' : v === null ? 'null' : v.constructor.name;

ts 代码如下:

调用方式:

getType(new Set([1, 2, 3])); // 'Set'

应用场景

以下是一个实战示例:

结果如下:

<iframe src="codes/javascript/html/get-type.html"></iframe>