# kind-of 中文文档

规范地址：https://npmcn.com/package/kind-of/

版本：6\.0\.3
npm 元数据更新时间：2023-07-12T19:12:21.548Z
中文译文更新时间：2026-11-05T14:54:48.496Z
软件包许可：MIT

本文由上游 README 翻译，可能存在偏差，请结合原文核对 API、命令与配置。版权与许可归原作者所有。

原始 npm 页面：https://www.npmjs.com/package/kind-of
原文对照：https://npmcn.com/package/kind-of/?lang=original
项目主页：https://github.com/jonschlinkert/kind-of
源码仓库：https://github.com/jonschlinkert/kind-of

## 关键信息（来自 npm 元数据）

- 包体积（解压后）：22.8 kB，5 个文件
- 模块格式：CommonJS
- TypeScript 类型：未声明
- Node.js 要求：\>=0\.10\.0
- 运行依赖：0 个
- 首次发布：2014-09-26T08:08:38.913Z
- 最新版本发布：2020-01-16T16:59:19.808Z
- 近 30 天下载量：438975338（2026-10-05 至 2026-11-03，npm 官方下载统计）
- 已发布版本数：27（近 12 个月 0 个）

## 中文简介

获取一个值的本地类型 .

## 安装

```sh
npm install kind-of
```

也可以使用：`pnpm add kind-of`、`yarn add kind-of`、`bun add kind-of`

## README

# 类型 [![NPM version](https://img.shields.io/npm/v/kind-of.svg?style=flat)](https://www.npmjs.com/package/kind-of) [![NPM monthly downloads](https://img.shields.io/npm/dm/kind-of.svg?style=flat)](https://npmjs.org/package/kind-of) [![NPM total downloads](https://img.shields.io/npm/dt/kind-of.svg?style=flat)](https://npmjs.org/package/kind-of) [![Linux Build Status](https://img.shields.io/travis/jonschlinkert/kind-of.svg?style=flat&label=Travis)](https://travis-ci.org/jonschlinkert/kind-of)

> 获取一个值的本地类型 .

请考虑跟随这个项目的作者, [乔恩·斯林克特](https://github.com/jonschlinkert),并考虑主演该项目以显示你的:心:和支持.

## 安装

安装方式 [无](https://www.npmjs.com/)数字 :

```sh
$ npm install --save kind-of
```

安装方式 [箭头](https://bower.io/)

```sh
$ bower install kind-of --save
```

## 为什么用这个?

1.  [这么快](https://npmcn.com/package/kind-of/#readme-benchmarks) | [优化](https://npmcn.com/package/kind-of/#readme-optimizations)
2.  [更好的类型检查](https://npmcn.com/package/kind-of/#readme-better-type-checking)

## 使用量

> es5,es6,浏览器准备好

```js
var kindOf = require('kind-of');

kindOf(undefined);
//=> 'undefined'

kindOf(null);
//=> 'null'

kindOf(true);
//=> 'boolean'

kindOf(false);
//=> 'boolean'

kindOf(new Buffer(''));
//=> 'buffer'

kindOf(42);
//=> 'number'

kindOf('str');
//=> 'string'

kindOf(arguments);
//=> 'arguments'

kindOf({});
//=> 'object'

kindOf(Object.create(null));
//=> 'object'

kindOf(new Test());
//=> 'object'

kindOf(new Date());
//=> 'date'

kindOf([1, 2, 3]);
//=> 'array'

kindOf(/foo/);
//=> 'regexp'

kindOf(new RegExp('foo'));
//=> 'regexp'

kindOf(new Error('error'));
//=> 'error'

kindOf(function () {});
//=> 'function'

kindOf(function * () {});
//=> 'generatorfunction'

kindOf(Symbol('str'));
//=> 'symbol'

kindOf(new Map());
//=> 'map'

kindOf(new WeakMap());
//=> 'weakmap'

kindOf(new Set());
//=> 'set'

kindOf(new WeakSet());
//=> 'weakset'

kindOf(new Int8Array());
//=> 'int8array'

kindOf(new Uint8Array());
//=> 'uint8array'

kindOf(new Uint8ClampedArray());
//=> 'uint8clampedarray'

kindOf(new Int16Array());
//=> 'int16array'

kindOf(new Uint16Array());
//=> 'uint16array'

kindOf(new Int32Array());
//=> 'int32array'

kindOf(new Uint32Array());
//=> 'uint32array'

kindOf(new Float32Array());
//=> 'float32array'

kindOf(new Float64Array());
//=> 'float64array'
```

## 基准

基准 [类型](http://github.com/CodingFu/typeof) 财务报告和审定财务报表 [类型](https://github.com/ForbesLindesay/type-of)。 。 。 .

```bash
# arguments (32 bytes)
  kind-of x 17,024,098 ops/sec ±1.90% (86 runs sampled)
  lib-type-of x 11,926,235 ops/sec ±1.34% (83 runs sampled)
  lib-typeof x 9,245,257 ops/sec ±1.22% (87 runs sampled)

  fastest is kind-of (by 161% avg)

# array (22 bytes)
  kind-of x 17,196,492 ops/sec ±1.07% (88 runs sampled)
  lib-type-of x 8,838,283 ops/sec ±1.02% (87 runs sampled)
  lib-typeof x 8,677,848 ops/sec ±0.87% (87 runs sampled)

  fastest is kind-of (by 196% avg)

# boolean (24 bytes)
  kind-of x 16,841,600 ops/sec ±1.10% (86 runs sampled)
  lib-type-of x 8,096,787 ops/sec ±0.95% (87 runs sampled)
  lib-typeof x 8,423,345 ops/sec ±1.15% (86 runs sampled)

  fastest is kind-of (by 204% avg)

# buffer (38 bytes)
  kind-of x 14,848,060 ops/sec ±1.05% (86 runs sampled)
  lib-type-of x 3,671,577 ops/sec ±1.49% (87 runs sampled)
  lib-typeof x 8,360,236 ops/sec ±1.24% (86 runs sampled)

  fastest is kind-of (by 247% avg)

# date (30 bytes)
  kind-of x 16,067,761 ops/sec ±1.58% (86 runs sampled)
  lib-type-of x 8,954,436 ops/sec ±1.40% (87 runs sampled)
  lib-typeof x 8,488,307 ops/sec ±1.51% (84 runs sampled)

  fastest is kind-of (by 184% avg)

# error (36 bytes)
  kind-of x 9,634,090 ops/sec ±1.12% (89 runs sampled)
  lib-type-of x 7,735,624 ops/sec ±1.32% (86 runs sampled)
  lib-typeof x 7,442,160 ops/sec ±1.11% (90 runs sampled)

  fastest is kind-of (by 127% avg)

# function (34 bytes)
  kind-of x 10,031,494 ops/sec ±1.27% (86 runs sampled)
  lib-type-of x 9,502,757 ops/sec ±1.17% (89 runs sampled)
  lib-typeof x 8,278,985 ops/sec ±1.08% (88 runs sampled)

  fastest is kind-of (by 113% avg)

# null (24 bytes)
  kind-of x 18,159,808 ops/sec ±1.92% (86 runs sampled)
  lib-type-of x 12,927,635 ops/sec ±1.01% (88 runs sampled)
  lib-typeof x 7,958,234 ops/sec ±1.21% (89 runs sampled)

  fastest is kind-of (by 174% avg)

# number (22 bytes)
  kind-of x 17,846,779 ops/sec ±0.91% (85 runs sampled)
  lib-type-of x 3,316,636 ops/sec ±1.19% (86 runs sampled)
  lib-typeof x 2,329,477 ops/sec ±2.21% (85 runs sampled)

  fastest is kind-of (by 632% avg)

# object-plain (47 bytes)
  kind-of x 7,085,155 ops/sec ±1.05% (88 runs sampled)
  lib-type-of x 8,870,930 ops/sec ±1.06% (83 runs sampled)
  lib-typeof x 8,716,024 ops/sec ±1.05% (87 runs sampled)

  fastest is lib-type-of (by 112% avg)

# regex (25 bytes)
  kind-of x 14,196,052 ops/sec ±1.65% (84 runs sampled)
  lib-type-of x 9,554,164 ops/sec ±1.25% (88 runs sampled)
  lib-typeof x 8,359,691 ops/sec ±1.07% (87 runs sampled)

  fastest is kind-of (by 158% avg)

# string (33 bytes)
  kind-of x 16,131,428 ops/sec ±1.41% (85 runs sampled)
  lib-type-of x 7,273,172 ops/sec ±1.05% (87 runs sampled)
  lib-typeof x 7,382,635 ops/sec ±1.17% (85 runs sampled)

  fastest is kind-of (by 220% avg)

# symbol (34 bytes)
  kind-of x 17,011,537 ops/sec ±1.24% (86 runs sampled)
  lib-type-of x 3,492,454 ops/sec ±1.23% (89 runs sampled)
  lib-typeof x 7,471,235 ops/sec ±2.48% (87 runs sampled)

  fastest is kind-of (by 310% avg)

# template-strings (36 bytes)
  kind-of x 15,434,250 ops/sec ±1.46% (83 runs sampled)
  lib-type-of x 7,157,907 ops/sec ±0.97% (87 runs sampled)
  lib-typeof x 7,517,986 ops/sec ±0.92% (86 runs sampled)

  fastest is kind-of (by 210% avg)

# undefined (29 bytes)
  kind-of x 19,167,115 ops/sec ±1.71% (87 runs sampled)
  lib-type-of x 15,477,740 ops/sec ±1.63% (85 runs sampled)
  lib-typeof x 19,075,495 ops/sec ±1.17% (83 runs sampled)

  fastest is lib-typeof,kind-of

```

## 优化

在8个案件中的7个案件中,这个图书馆比列入基准的其他顶级图书馆快了2x-10x. 有一些东西导致这种性能优势,没有硬而快的规则,但几乎所有代码库中都简单而可重复:

1.  首先优化最快和最常见的使用案例。 当然,这将会改变 从项目到项目, 但我花了一些时间来理解为什么 `typeof` 支票被用在我的图书馆 我经常使用的其他图书馆.
2.  围绕瓶颈实现优化 - 换句话说,执行条件的顺序是显著的,因为每次检查都和之前的失败检查一样快. 这里,迄今为止最大的瓶颈是检查平地物体(由该物体所创建) `Object` 建筑师). 我选择了通过消灭过程而不是通过前面的野蛮武力(例如使用类似的东西)来实现这种检查 `val.constructor.name`),这样,所有其他类型的检查都不会受到处罚.
3.  不要做不必要处理 - 为什么 `.slice(8, -1).toLowerCase();` 只是为了得到消息 `regex`它的更快做到这一点 `if (type === '[object RegExp]') return 'regex'`
4.  没有理由在微lib中尽可能地使代码为terse,仅仅为了让代码更短而赢得分数. 偏好表演密码比特尔斯密码好 你总是只用一个 `require()` 语句,不管代码是怎样写的.

## 更好的类型检查

似乎比我看过的其他类型检查液态更一致 例如,这里有一些不同的结果来自其他民众的解放:

### [类型](https://github.com/CodingFu/typeof) 独立

无法正确识别自定义构造器实例( 相当常见 ) :

```js
var typeOf = require('typeof');
function Test() {}
console.log(typeOf(new Test()));
//=> 'test'
```

回返 `object` 改为 `arguments`数字 :

```js
function foo() {
  console.log(typeOf(arguments)) //=> 'object'
}
foo();
```

### [类型](https://github.com/ForbesLindesay/type-of) 独立

返回错误 `object` 用于生成器函数、缓冲器、, `Map`, (中文(简体) ), `Set`, (中文(简体) ), `WeakMap` 财务报告和审定财务报表 `WeakSet`数字 :

```js
function * foo() {}
console.log(typeOf(foo));
//=> 'object'
console.log(typeOf(new Buffer('')));
//=> 'object'
console.log(typeOf(new Map()));
//=> 'object'
console.log(typeOf(new Set()));
//=> 'object'
console.log(typeOf(new WeakMap()));
//=> 'object'
console.log(typeOf(new WeakSet()));
//=> 'object'
```

## 关于

**捐款**

拉出请求和星星总是受欢迎的. 对于错误和特性请求, [请创建一个问题](https://npmcn.com/issues/new)。 。 。 .

**运行测试**

运行和审查单元测试是熟悉图书馆及其API的好办法。 您可以使用以下命令安装依赖性并运行测试:

```sh
$ npm install && npm test
```
**大楼文件**

_(此工程的 readme.md 由 [动词](https://github.com/verbose/verb-generate-readme)请不要直接编辑读取器。 读取器的任何改动必须在 [动词 md](https://npmcn.com/package/kind-of/.verb.md) 读取我的模板。 )_

要生成读取, 请运行以下命令 :

```sh
$ npm install -g verbose/verb#dev verb-generate-readme && verb
```

### 相关项目

您可能也对这些项目感兴趣:

-   [正在显示](https://www.npmjs.com/package/is-glob): 回归 `true` 如果给定的字符串看起来像是光彩图案或外形图案.. [更多](https://github.com/micromatch/is-glob) | [主页](https://github.com/micromatch/is-glob "Returns `true` if the given string looks like a glob pattern or an extglob pattern. This makes it easy to create code that only uses external modules like node-glob when necessary, resulting in much faster code execution and initialization time, and a bet")
-   [是数](https://www.npmjs.com/package/is-number): 如果一个数字或字符串值是有限数字,则返回正数。 对regex有用.. [更多](https://github.com/jonschlinkert/is-number) | [主页](https://github.com/jonschlinkert/is-number "Returns true if a number or string value is a finite number. Useful for regex matches, parsing, user input, etc.")
-   [是原始内容](https://www.npmjs.com/package/is-primitive): 回归 `true` 如果数值是原始的 [主页](https://github.com/jonschlinkert/is-primitive "Returns `true` if the value is a primitive. ")

### 贡献者

| **提交** | **贡献者** |  
| -- -- -- -- -- -- --  
102个 [扬施林克特](https://github.com/jonschlinkert) |  
| 3 | [域码](https://github.com/aretecode) |  
|2| [米格尔莫塔](https://github.com/miguelmota) |  
| 1 | [鸽子](https://github.com/doowb) |  
| 1 | [dtothefp 软件](https://github.com/dtothefp) |  
| 1 | [扬·斯通克勒](https://github.com/ianstormtaylor) |  
| 1 | [克谢德洛](https://github.com/ksheedlo) |  
| 1 | [脚下](https://github.com/pdehaan) |  
| 1 | [后退读取](https://github.com/laggingreflex) |  
| 1 | [tunncko 企业](https://github.com/tunnckoCore) |  
| 1 | [霞风9号](https://github.com/xiaofen9) |

### 作者

**乔恩·斯林克特**

-   [GitHub 配置文件](https://github.com/jonschlinkert)
-   [推特简介](https://twitter.com/jonschlinkert)
-   [链接到配置文件](https://linkedin.com/in/jonschlinkert)

### 许可证

版权:2020年, [乔恩·斯林克特](https://github.com/jonschlinkert)。 。 。 . 释放 [MIT 许可证](https://npmcn.com/package/kind-of/LICENSE)。 。 。 .

* * *

_此文件由 [动词基因读取器](https://github.com/verbose/verb-generate-readme)v0.8.0,于2020年1月16日查阅._

## 常见问题

### kind-of 是什么？

获取一个值的本地类型 \.

### 如何安装 kind-of？

在项目目录运行 npm install kind-of，或使用 pnpm add / yarn add / bun add。

### kind-of 支持 TypeScript 吗？

该包的 package\.json 没有声明类型入口。需要类型提示时，可以在 npm 上查找社区维护的 @types/kind-of 是否存在。

### kind-of 支持 ESM 还是 CommonJS？

以 CommonJS 形式发布，可以直接 require\(\)；在 ES Module 项目中也可以用 import 引入（Node\.js 会把 module\.exports 作为默认导出）。

### kind-of 需要什么版本的 Node\.js？

package\.json 的 engines 字段要求 Node\.js \>=0\.10\.0。版本不符时 npm 默认只给出警告，开启 engine-strict 后会拒绝安装。

### kind-of 可以商用吗？

kind-of 使用 MIT 许可。宽松许可：允许商业使用、修改和再分发，通常只需在分发时保留原版权与许可声明。以上为概括，不构成法律意见，请以许可原文为准。

### kind-of 的下载量有多少？

根据 npm 官方下载统计，kind-of 在 2026/10/05 – 2026/11/03 这 30 天内被下载 438,975,338 次，日均约 14,632,511 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入，适合横向比较热度，不能直接等同于使用人数。

### kind-of 还在维护吗？

最新版本 6\.0\.3 发布于 2020/01/17（6 年前）；至今共发布 27 个版本，其中过去 12 个月发布了 0 个；首次发布于 2014/09/26。发布频率只是参考信号：功能稳定的工具可能很少需要更新，建议结合源码仓库的 issue 和提交记录判断。

