---
url: /docs/guide/usage/linter/rules/unicorn/prefer-code-point.md
---

### 它是做什么的

优先使用 `String#codePointAt` 而不是 `String#charCodeAt`。
优先使用 `String.fromCodePoint` 而不是 `String.fromCharCode`。

### 为什么这不好？

Unicode 在 [`String#codePointAt()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/codePointAt) 和 [`String.fromCodePoint()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/fromCodePoint) 中支持得更好。

[`String.fromCodePoint()` 和 `String.fromCharCode()` 的区别](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/fromCodePoint#compared_to_fromcharcode)

### 示例

此规则的**错误**代码示例：

```javascript
"🦄".charCodeAt(0);
String.fromCharCode(0x1f984);
```

此规则的**正确**代码示例：

```javascript
"🦄".codePointAt(0);
String.fromCodePoint(0x1f984);
```

## 如何使用

## 版本

此规则是在 v0.0.16 中添加的。

## 参考资料
