2018-04-02 11:50:51 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
## 字符串的扩展
|
|
|
|
|
|
|
|
|
|
ES6中的字符串扩展,用得少,而且逻辑相对简单。如下:
|
|
|
|
|
|
|
|
|
|
- `includes(str)`:判断是否包含指定的字符串
|
|
|
|
|
|
|
|
|
|
- `startsWith(str)`:判断是否以指定字符串开头
|
|
|
|
|
|
|
|
|
|
- `endsWith(str)`:判断是否以指定字符串结尾
|
|
|
|
|
|
|
|
|
|
- `repeat(count)`:重复指定次数
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
举例如下:
|
|
|
|
|
|
|
|
|
|
```javascript
|
|
|
|
|
let str = 'abcdefg';
|
|
|
|
|
|
|
|
|
|
console.log(str.includes('a'));//true
|
|
|
|
|
console.log(str.includes('h'));//false
|
|
|
|
|
|
|
|
|
|
//startsWith(str) : 判断是否以指定字符串开头
|
|
|
|
|
console.log(str.startsWith('a'));//true
|
|
|
|
|
console.log(str.startsWith('d'));//false
|
|
|
|
|
|
|
|
|
|
//endsWith(str) : 判断是否以指定字符串结尾
|
|
|
|
|
console.log(str.endsWith('g'));//true
|
|
|
|
|
console.log(str.endsWith('d'));//false
|
|
|
|
|
|
|
|
|
|
//repeat(count) : 重复指定次数a
|
|
|
|
|
console.log(str.repeat(5));
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
打印结果:
|
|
|
|
|
|
2018-06-11 21:11:23 +08:00
|
|
|
|
![](http://img.smyhvae.com/20180402_1050.png)
|
2018-04-02 11:50:51 +08:00
|
|
|
|
|
|
|
|
|
## 数值的扩展
|
|
|
|
|
|
|
|
|
|
- 二进制与八进制数值表示法: 二进制用`0b`, 八进制用`0o`。
|
|
|
|
|
|
|
|
|
|
举例:
|
|
|
|
|
|
|
|
|
|
```javascript
|
|
|
|
|
console.log(0b1010);//10
|
|
|
|
|
console.log(0o56);//46
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
- `Number.isFinite(i)`:判断是否为有限大的数。比如`Infinity`这种无穷大的数,返回的就是false。
|
|
|
|
|
|
|
|
|
|
- `Number.isNaN(i)`:判断是否为NaN。
|
|
|
|
|
|
|
|
|
|
- `Number.isInteger(i)`:判断是否为整数。
|
|
|
|
|
|
|
|
|
|
- `Number.parseInt(str)`:将字符串转换为对应的数值。
|
|
|
|
|
|
|
|
|
|
- `Math.trunc(i)`:去除小数部分。
|
|
|
|
|
|
|
|
|
|
举例:
|
|
|
|
|
|
|
|
|
|
```javascript
|
|
|
|
|
//Number.isFinite(i) : 判断是否是有限大的数
|
|
|
|
|
console.log(Number.isFinite(NaN)); //false
|
|
|
|
|
console.log(Number.isFinite(5)); //true
|
|
|
|
|
console.log(Number.isFinite(Infinity)); //false
|
|
|
|
|
|
|
|
|
|
//Number.isNaN(i) : 判断是否是NaN
|
|
|
|
|
console.log(Number.isNaN(NaN));//true
|
|
|
|
|
console.log(Number.isNaN(5));//falsse
|
|
|
|
|
|
|
|
|
|
//Number.isInteger(i) : 判断是否是整数
|
|
|
|
|
console.log(Number.isInteger(5.23));//false
|
|
|
|
|
console.log(Number.isInteger(5.0));//true
|
|
|
|
|
console.log(Number.isInteger(5));//true
|
|
|
|
|
|
|
|
|
|
//Number.parseInt(str) : 将字符串转换为对应的数值
|
|
|
|
|
console.log(Number.parseInt('123abc'));//123
|
|
|
|
|
console.log(Number.parseInt('a123abc'));//NaN
|
|
|
|
|
|
|
|
|
|
// Math.trunc(i) : 直接去除小数部分
|
|
|
|
|
console.log(Math.trunc(13.123));//13
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
## 数组的扩展
|
|
|
|
|
|
|
|
|
|
### 扩展1
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
```javascript
|
|
|
|
|
Array.from(伪数组/可遍历的对象)
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
**作用**:将伪数组对象或可遍历对象转换为真数组。
|
|
|
|
|
|
|
|
|
|
**举例:**
|
|
|
|
|
|
|
|
|
|
```html
|
|
|
|
|
<body>
|
|
|
|
|
<button>按钮1</button>
|
|
|
|
|
<button>按钮2</button>
|
|
|
|
|
<button>按钮3</button>
|
|
|
|
|
|
|
|
|
|
<script>
|
|
|
|
|
let btnArray = document.getElementsByTagName('button');
|
|
|
|
|
console.log(btnArray);
|
|
|
|
|
console.log(btnArray[0]);
|
|
|
|
|
|
|
|
|
|
</script>
|
|
|
|
|
|
|
|
|
|
</body>
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
上面的布局中,有三个button标签,我们通过`getElementsByTagName`获取到的`btnArray`实际上是**伪数组**,并不是真实的数组:
|
|
|
|
|
|
2018-06-11 21:11:23 +08:00
|
|
|
|
![](http://img.smyhvae.com/20180402_1116.png)
|
2018-04-02 11:50:51 +08:00
|
|
|
|
|
|
|
|
|
既然`btnArray`是伪数组,它就不能使用数组的一般方法,否则会报错:
|
|
|
|
|
|
2018-06-11 21:11:23 +08:00
|
|
|
|
![](http://img.smyhvae.com/20180402_1121.png)
|
2018-04-02 11:50:51 +08:00
|
|
|
|
|
|
|
|
|
解决办法:采用`Array.from`方法将`btnArray`这个伪数组转换为真数组即可:
|
|
|
|
|
|
|
|
|
|
```javascript
|
|
|
|
|
Array.from(btnArray);
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
然后就可以使用数组的一般方法了:
|
|
|
|
|
|
2018-06-11 21:11:23 +08:00
|
|
|
|
![](http://img.smyhvae.com/20180402_1125.png)
|
2018-04-02 11:50:51 +08:00
|
|
|
|
|
|
|
|
|
### 扩展2
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
```javascript
|
|
|
|
|
Array.of(value1, value2, value3)
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
**作用**:将一系列值转换成数组。
|
|
|
|
|
|
|
|
|
|
**举例**:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
```javascript
|
|
|
|
|
//Array.of(value1, value2, value3) : 将一系列值转换成数组
|
|
|
|
|
let arr = Array.of(1, 'abc', true);
|
|
|
|
|
console.log(arr);
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
### 扩展3
|
|
|
|
|
|
2018-04-04 22:58:12 +08:00
|
|
|
|
**方法1**:
|
2018-04-02 11:50:51 +08:00
|
|
|
|
|
|
|
|
|
|
2018-04-04 22:58:12 +08:00
|
|
|
|
```javascript
|
|
|
|
|
find(function(item, index, arr){return true})
|
|
|
|
|
```
|
2018-04-02 11:50:51 +08:00
|
|
|
|
|
2018-04-04 22:58:12 +08:00
|
|
|
|
**作用**:找出第一个满足「指定条件返回true」的元素。
|
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
|
|
|
|
|
let arr = [2, 3, 2, 5, 7, 6];
|
2018-04-02 11:50:51 +08:00
|
|
|
|
|
2018-04-04 22:58:12 +08:00
|
|
|
|
let result = arr.find(function (item, index) {
|
|
|
|
|
return item > 4; //遍历数组arr,一旦发现有第一个元素大于4,就把这个元素返回
|
|
|
|
|
});
|
2018-04-02 11:50:51 +08:00
|
|
|
|
|
2018-04-04 22:58:12 +08:00
|
|
|
|
console.log(result); //打印结果:5
|
|
|
|
|
```
|
2018-04-02 11:50:51 +08:00
|
|
|
|
|
2018-04-04 22:58:12 +08:00
|
|
|
|
**方法2**:
|
2018-04-02 11:50:51 +08:00
|
|
|
|
|
2018-04-04 22:58:12 +08:00
|
|
|
|
```javascript
|
|
|
|
|
findIndex(function(item, index, arr){return true})
|
|
|
|
|
```
|
2018-04-02 11:50:51 +08:00
|
|
|
|
|
2018-04-04 22:58:12 +08:00
|
|
|
|
**作用**:找出第一个满足「指定条件返回true」的元素的index。
|
2018-04-02 11:50:51 +08:00
|
|
|
|
|
2018-04-04 22:58:12 +08:00
|
|
|
|
举例:
|
|
|
|
|
|
|
|
|
|
> 我们直接把上面的代码中的find方法改成findIndex即可。
|
2018-04-02 11:50:51 +08:00
|
|
|
|
|
|
|
|
|
```javascript
|
2018-04-04 22:58:12 +08:00
|
|
|
|
let arr = [2, 3, 2, 5, 7, 6];
|
|
|
|
|
|
|
|
|
|
let result = arr.findIndex(function (item, index) {
|
|
|
|
|
return item > 4; //遍历数组arr,一旦发现有第一个元素大于4,就把这个元素的index返回
|
|
|
|
|
});
|
2018-04-02 11:50:51 +08:00
|
|
|
|
|
2018-04-04 22:58:12 +08:00
|
|
|
|
console.log(result); //打印结果:3
|
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
|
|
|
|
### 扩展1
|
2018-04-02 11:50:51 +08:00
|
|
|
|
|
|
|
|
|
|
2018-04-04 22:58:12 +08:00
|
|
|
|
```javascript
|
|
|
|
|
Object.is(v1, v2)
|
|
|
|
|
```
|
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
|
|
|
|
|
console.log(0 == -0);
|
|
|
|
|
console.log(NaN == 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
|
|
|
|
|
2018-04-04 22:58:12 +08:00
|
|
|
|
```
|
|
|
|
|
true
|
|
|
|
|
false
|
|
|
|
|
```
|
2018-04-02 11:50:51 +08:00
|
|
|
|
|
2018-04-04 22:58:12 +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
|
2018-04-04 22:58:12 +08:00
|
|
|
|
console.log(Object.is(0, -0));
|
|
|
|
|
console.log(Object.is(NaN, NaN));
|
|
|
|
|
```
|
2018-04-02 11:50:51 +08:00
|
|
|
|
|
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
|
|
|
|
|
2018-04-04 22:58:12 +08:00
|
|
|
|
### 扩展2(重要)
|
2018-04-02 11:50:51 +08:00
|
|
|
|
|
2018-04-04 22:58:12 +08:00
|
|
|
|
```javascript
|
|
|
|
|
Object.assign(目标对象, 源对象1, 源对象2...)
|
|
|
|
|
```
|
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
|
|
|
|
|
let obj1 = { name: 'smyhvae', age: 26 };
|
|
|
|
|
let obj2 = { city: 'shenzhen' };
|
|
|
|
|
let obj3 = {};
|
2018-04-02 11:50:51 +08:00
|
|
|
|
|
2018-04-04 22:58:12 +08:00
|
|
|
|
Object.assign(obj3, obj1, obj2);
|
|
|
|
|
console.log(obj3);
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
打印结果:
|
|
|
|
|
|
2018-06-11 21:11:23 +08:00
|
|
|
|
![](http://img.smyhvae.com/20180404_2240.png)
|
2018-04-02 11:50:51 +08:00
|
|
|
|
|
2018-04-04 22:58:12 +08:00
|
|
|
|
上图显示,成功将obj1和obj2的属性复制给了obj3。
|
2018-04-02 11:50:51 +08:00
|
|
|
|
|
2018-04-04 22:58:12 +08:00
|
|
|
|
### 扩展3:`__proto__`属性
|
|
|
|
|
|
|
|
|
|
举例:
|
2018-04-02 11:50:51 +08:00
|
|
|
|
|
|
|
|
|
```javascript
|
2018-04-04 22:58:12 +08:00
|
|
|
|
let obj1 = {name:'smyhvae'};
|
|
|
|
|
let obj2 = {};
|
2018-04-02 11:50:51 +08:00
|
|
|
|
|
2018-04-04 22:58:12 +08:00
|
|
|
|
obj2.__proto__ = obj1;
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
上方代码中,obj2本身是没有属性的,但是通过`__proto__`属性和obj1产生关联,于是就可以获得obj1里的属性。
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2018-04-02 11:50:51 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2018-04-04 22:58:12 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
```javascript
|
|
|
|
|
|
|
|
|
|
```
|
2018-04-02 11:50:51 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|