2018-04-02 11:50:51 +08:00
|
|
|
|
## 字符串的扩展
|
|
|
|
|
|
2020-08-25 20:47:55 +08:00
|
|
|
|
> 下面提到的字符串的几个方法,更详细的内容,可以看《04-JavaScript 基础/内置对象 String:字符串的常见方法.md》。
|
2018-04-02 11:50:51 +08:00
|
|
|
|
|
2020-08-25 20:47:55 +08:00
|
|
|
|
ES6 中的字符串扩展如下:
|
2018-04-02 11:50:51 +08:00
|
|
|
|
|
2020-08-25 20:47:55 +08:00
|
|
|
|
- `includes(str)`:判断是否包含指定的字符串
|
2018-04-02 11:50:51 +08:00
|
|
|
|
|
2020-08-25 20:47:55 +08:00
|
|
|
|
- `startsWith(str)`:判断是否以指定字符串开头
|
2018-04-02 11:50:51 +08:00
|
|
|
|
|
2020-08-25 20:47:55 +08:00
|
|
|
|
- `endsWith(str)`:判断是否以指定字符串结尾
|
2018-04-02 11:50:51 +08:00
|
|
|
|
|
2020-08-25 20:47:55 +08:00
|
|
|
|
- `repeat(count)`:重复指定次数
|
2018-04-02 11:50:51 +08:00
|
|
|
|
|
|
|
|
|
举例如下:
|
|
|
|
|
|
|
|
|
|
```javascript
|
2020-08-25 20:47:55 +08:00
|
|
|
|
let str = 'abcdefg';
|
2018-04-02 11:50:51 +08:00
|
|
|
|
|
2020-08-25 20:47:55 +08:00
|
|
|
|
console.log(str.includes('a')); //true
|
|
|
|
|
console.log(str.includes('h')); //false
|
2018-04-02 11:50:51 +08:00
|
|
|
|
|
2020-08-25 20:47:55 +08:00
|
|
|
|
//startsWith(str) : 判断是否以指定字符串开头
|
|
|
|
|
console.log(str.startsWith('a')); //true
|
|
|
|
|
console.log(str.startsWith('d')); //false
|
2018-04-02 11:50:51 +08:00
|
|
|
|
|
2020-08-25 20:47:55 +08:00
|
|
|
|
//endsWith(str) : 判断是否以指定字符串结尾
|
|
|
|
|
console.log(str.endsWith('g')); //true
|
|
|
|
|
console.log(str.endsWith('d')); //false
|
2018-04-02 11:50:51 +08:00
|
|
|
|
|
2020-08-25 20:47:55 +08:00
|
|
|
|
//repeat(count) : 重复指定次数a
|
|
|
|
|
console.log(str.repeat(5));
|
2018-04-02 11:50:51 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
打印结果:
|
|
|
|
|
|
2018-06-11 21:11:23 +08:00
|
|
|
|
![](http://img.smyhvae.com/20180402_1050.png)
|
2018-04-02 11:50:51 +08:00
|
|
|
|
|
2019-09-20 20:19:36 +08:00
|
|
|
|
## Number 的扩展
|
2018-04-02 11:50:51 +08:00
|
|
|
|
|
2020-08-25 20:47:55 +08:00
|
|
|
|
- 二进制与八进制数值表示法: 二进制用`0b`, 八进制用`0o`。
|
2018-04-02 11:50:51 +08:00
|
|
|
|
|
|
|
|
|
举例:
|
|
|
|
|
|
|
|
|
|
```javascript
|
2020-08-25 20:47:55 +08:00
|
|
|
|
console.log(0b1010); //10
|
|
|
|
|
console.log(0o56); //46
|
2018-04-02 11:50:51 +08:00
|
|
|
|
```
|
|
|
|
|
|
2020-08-25 20:47:55 +08:00
|
|
|
|
- `Number.isFinite(i)`:判断是否为有限大的数。比如`Infinity`这种无穷大的数,返回的就是 false。
|
2018-04-02 11:50:51 +08:00
|
|
|
|
|
2020-08-25 20:47:55 +08:00
|
|
|
|
- `Number.isNaN(i)`:判断是否为 NaN。
|
2018-04-02 11:50:51 +08:00
|
|
|
|
|
2020-08-25 20:47:55 +08:00
|
|
|
|
- `Number.isInteger(i)`:判断是否为整数。
|
2018-04-02 11:50:51 +08:00
|
|
|
|
|
2020-08-25 20:47:55 +08:00
|
|
|
|
- `Number.parseInt(str)`:将字符串转换为对应的数值。
|
2018-04-02 11:50:51 +08:00
|
|
|
|
|
2020-08-25 20:47:55 +08:00
|
|
|
|
- `Math.trunc(i)`:去除小数部分。
|
2018-04-02 11:50:51 +08:00
|
|
|
|
|
|
|
|
|
举例:
|
|
|
|
|
|
|
|
|
|
```javascript
|
2020-08-25 20:47:55 +08:00
|
|
|
|
//Number.isFinite(i) : 判断是否是有限大的数
|
|
|
|
|
console.log(Number.isFinite(NaN)); //false
|
|
|
|
|
console.log(Number.isFinite(5)); //true
|
|
|
|
|
console.log(Number.isFinite(Infinity)); //false
|
2018-04-02 11:50:51 +08:00
|
|
|
|
|
2020-08-25 20:47:55 +08:00
|
|
|
|
//Number.isNaN(i) : 判断是否是NaN
|
|
|
|
|
console.log(Number.isNaN(NaN)); //true
|
|
|
|
|
console.log(Number.isNaN(5)); //falsse
|
2018-04-02 11:50:51 +08:00
|
|
|
|
|
2020-08-25 20:47:55 +08:00
|
|
|
|
//Number.isInteger(i) : 判断是否是整数
|
|
|
|
|
console.log(Number.isInteger(5.23)); //false
|
|
|
|
|
console.log(Number.isInteger(5.0)); //true
|
|
|
|
|
console.log(Number.isInteger(5)); //true
|
2018-04-02 11:50:51 +08:00
|
|
|
|
|
2020-08-25 20:47:55 +08:00
|
|
|
|
//Number.parseInt(str) : 将字符串转换为对应的数值
|
|
|
|
|
console.log(Number.parseInt('123abc')); //123
|
|
|
|
|
console.log(Number.parseInt('a123abc')); //NaN
|
2018-04-02 11:50:51 +08:00
|
|
|
|
|
2020-08-25 20:47:55 +08:00
|
|
|
|
// Math.trunc(i) : 直接去除小数部分
|
|
|
|
|
console.log(Math.trunc(13.123)); //13
|
2018-04-02 11:50:51 +08:00
|
|
|
|
```
|
|
|
|
|
|
2020-08-25 20:47:55 +08:00
|
|
|
|
## 数组的扩展
|
2018-04-02 11:50:51 +08:00
|
|
|
|
|
2020-08-25 20:47:55 +08:00
|
|
|
|
> 下面提到的数组的几个方法,更详细的内容,可以看《04-JavaScript 基础/数组的常见方法.md》。
|
2018-04-02 11:50:51 +08:00
|
|
|
|
|
2020-08-25 20:47:55 +08:00
|
|
|
|
- Array.from()
|
2018-04-02 11:50:51 +08:00
|
|
|
|
|
2020-08-25 20:47:55 +08:00
|
|
|
|
- find()
|
2018-04-02 11:50:51 +08:00
|
|
|
|
|
2020-08-25 20:47:55 +08:00
|
|
|
|
- findIndex()
|
2018-04-02 11:50:51 +08:00
|
|
|
|
|
2018-04-04 22:58:12 +08:00
|
|
|
|
## 对象的扩展
|
2018-04-02 11:50:51 +08:00
|
|
|
|
|
2020-08-25 20:47:55 +08:00
|
|
|
|
### 扩展 1
|
2018-04-02 11:50:51 +08:00
|
|
|
|
|
2018-04-04 22:58:12 +08:00
|
|
|
|
```javascript
|
2020-08-25 20:47:55 +08:00
|
|
|
|
Object.is(v1, v2);
|
2018-04-04 22:58:12 +08:00
|
|
|
|
```
|
2018-04-02 11:50:51 +08:00
|
|
|
|
|
2018-04-04 22:58:12 +08:00
|
|
|
|
**作用:**判断两个数据是否完全相等。底层是通过**字符串**来判断的。
|
2018-04-02 11:50:51 +08:00
|
|
|
|
|
2018-04-04 22:58:12 +08:00
|
|
|
|
我们先来看下面这两行代码的打印结果:
|
2018-04-02 11:50:51 +08:00
|
|
|
|
|
2018-04-04 22:58:12 +08:00
|
|
|
|
```javascript
|
2020-08-25 20:47:55 +08:00
|
|
|
|
console.log(0 == -0);
|
|
|
|
|
console.log(NaN == NaN);
|
2018-04-04 22:58:12 +08:00
|
|
|
|
```
|
2018-04-02 11:50:51 +08:00
|
|
|
|
|
2018-04-04 22:58:12 +08:00
|
|
|
|
打印结果:
|
2018-04-02 11:50:51 +08:00
|
|
|
|
|
2018-04-04 22:58:12 +08:00
|
|
|
|
```
|
|
|
|
|
true
|
|
|
|
|
false
|
|
|
|
|
```
|
2018-04-02 11:50:51 +08:00
|
|
|
|
|
2020-08-25 20:47:55 +08:00
|
|
|
|
上方代码中,第一行代码的打印结果为 true,这个很好理解。第二行代码的打印结果为 false,因为 NaN 和任何值都不相等。
|
2018-04-02 11:50:51 +08:00
|
|
|
|
|
2018-04-04 22:58:12 +08:00
|
|
|
|
但是,如果换成下面这种方式来比较:
|
2018-04-02 11:50:51 +08:00
|
|
|
|
|
|
|
|
|
```javascript
|
2020-08-25 20:47:55 +08:00
|
|
|
|
console.log(Object.is(0, -0));
|
|
|
|
|
console.log(Object.is(NaN, NaN));
|
2018-04-04 22:58:12 +08:00
|
|
|
|
```
|
2018-04-02 11:50:51 +08:00
|
|
|
|
|
2018-04-04 22:58:12 +08:00
|
|
|
|
打印结果却是:
|
|
|
|
|
|
2020-08-25 20:47:55 +08:00
|
|
|
|
```bash
|
2018-04-04 22:58:12 +08:00
|
|
|
|
false
|
|
|
|
|
true
|
2018-04-02 11:50:51 +08:00
|
|
|
|
```
|
|
|
|
|
|
2018-04-04 22:58:12 +08:00
|
|
|
|
代码解释:还是刚刚说的那样,`Object.is(v1, v2)`比较的是字符串是否相等。
|
2018-04-02 11:50:51 +08:00
|
|
|
|
|
2020-05-14 17:52:07 +08:00
|
|
|
|
### Object.assign()
|
|
|
|
|
|
2020-08-25 20:47:55 +08:00
|
|
|
|
Object.assign() 在实战开发中,使用到的频率非常高,一定要重视。关于它的内容,详见《04-JavaScript 基础/浅拷贝和深拷贝.md》。
|
2020-05-14 17:52:07 +08:00
|
|
|
|
|
2020-08-25 20:47:55 +08:00
|
|
|
|
### 扩展 3:`__proto__`属性
|
2018-04-04 22:58:12 +08:00
|
|
|
|
|
|
|
|
|
举例:
|
2018-04-02 11:50:51 +08:00
|
|
|
|
|
|
|
|
|
```javascript
|
2020-08-25 20:47:55 +08:00
|
|
|
|
let obj1 = { name: 'smyhvae' };
|
|
|
|
|
let obj2 = {};
|
2018-04-02 11:50:51 +08:00
|
|
|
|
|
2020-08-25 20:47:55 +08:00
|
|
|
|
obj2.__proto__ = obj1;
|
2018-04-04 22:58:12 +08:00
|
|
|
|
|
2020-08-25 20:47:55 +08:00
|
|
|
|
console.log(obj1);
|
|
|
|
|
console.log(obj2);
|
|
|
|
|
console.log(obj2.name);
|
2018-04-02 11:50:51 +08:00
|
|
|
|
```
|
|
|
|
|
|
2018-04-04 22:58:12 +08:00
|
|
|
|
打印结果:
|
|
|
|
|
|
2018-06-11 21:11:23 +08:00
|
|
|
|
![](http://img.smyhvae.com/20180404_2251.png)
|
2018-04-04 22:58:12 +08:00
|
|
|
|
|
2020-08-25 20:47:55 +08:00
|
|
|
|
上方代码中,obj2 本身是没有属性的,但是通过`__proto__`属性和 obj1 产生关联,于是就可以获得 obj1 里的属性。
|
2018-04-04 22:58:12 +08:00
|
|
|
|
|
|
|
|
|
```javascript
|
|
|
|
|
```
|
2021-05-24 12:43:12 +08:00
|
|
|
|
|
|
|
|
|
## 我的公众号
|
|
|
|
|
|
|
|
|
|
想学习**更多技能**?不妨关注我的微信公众号:**千古壹号**。
|
|
|
|
|
|
|
|
|
|
扫一扫,你将发现另一个全新的世界,而这将是一场美丽的意外:
|
|
|
|
|
|
2021-05-24 12:48:27 +08:00
|
|
|
|
![](https://img.smyhvae.com/20200102.png)
|