snowFlakeXue

Work hard for what you desire.


  • 首页

  • 关于

  • 分类

  • 归档

  • 搜索

Set和WeakSet

发表于 2019-08-01 | 分类于 ES6

Set和WeakSet都是数据结构而不是数据类型,他俩很相似,前者要放数组在他的数据结构里,后者要放对象在他的数据结构里。而放的方式,两者又有细微的差距。

阅读全文 »

Symbol在对象中作用

发表于 2019-08-01 | 分类于 ES6

symbol是ES6中新添加的一种数据类型,他属于原始数据类型,也就是存放在栈中,是按值传递。

阅读全文 »

对象

发表于 2019-07-25 | 更新于 2019-08-01 | 分类于 ES6

对象赋值

在ES5,如果我们在对象外声明变量,想让他作为对象里的值,我们需要些xx:xx,但在ES6中我们可以直接放进去

1
2
3
4
let a = 1;
let b = 2;
let ob = {a,b};
console.log(ob);

对象key值构建

当key是后台定义的,而不是前台确定的,那当我们使用时只需要一个中括号就可以解决

1
2
3
4
5
let key = 'a';
let ob = {
[key]:1
}
console.log(ob.a); //1

自定义对象方法

1
2
3
4
5
6
7
let ob = {
//匿名函数
add:function(a,b){
return a+b;
}
}
console.log(ob.add(1,2));

is比较方法

之前比较对象的元素是否相等我们用===,ES6引进了is方法,两者是有区别的,前者是同值相等后者是完全相等

1
2
3
4
5
6
7
8
let ob1 = {a:NaN};
let ob2 = {b:NaN};
let ob3 = {c:0};
let ob4 = {d:-0};
console.log(ob1.a===ob2.b);//false
console.log(ob3.c===ob4.d);//true
console.log(Object.is(ob1.a,ob2.b));//true
console.log(Object.is(ob3.c,ob4.d));//true

对象的连接

1
2
3
4
5
6
let ob1 = {a:NaN};
let ob2 = {b:NaN};
let ob3 = {c:0};
let ob4 = {d:-0};
let ob5 = Object.assign(ob1,ob2,ob3,ob4);
console.log(ob5);//{a: NaN, b: NaN, c: 0, d: -0}

函数和数组补漏

发表于 2019-07-24 | 分类于 ES6

对象的函数解构

当后端传来的数据时json对象格式,当我们想要让他作为函数的参数来调用时就可以用到,具体如下所示

1
2
3
4
5
6
let json = {
a:1,
b:2
}
let func = ({a,b=3})=>a+b;
console.log(func(json));//3

数组的函数解构

1
2
3
let arr =[1,2,3];
let func = (a,b,c)=>console.log(a,b,c);
func(...arr); //1 2 3

in

in的用处主要在于判断对象或数组里是否有元素,先来看看对象

1
2
3
4
5
let json = {
a:1,
a:2
}
console.log('a' in json);//true

关于数组,并不能确定有没有某个元素,但能确定某索引位置是否有元素,而在ES5我们知道,当我们用.length来查看是否有元素时,如果是空就会返回0,这显然是错误的,下面我们来看看ES6

1
2
let arr = ['','',''];
console.log(0 in arr);//true

数组循环方法

  1. forEach

这种方法会自动忽略掉空的情况,因此有的时候会出错

1
2
let arr = ['1','2','3'];
arr.forEach((val,index)=>console.log(index,val)); //注意顺序不能颠倒了
  1. filter
1
2
let arr = ['1','2','3'];
arr.filter(x=>console.log(x));//1 2 3
  1. some
1
2
let arr = ['1','2','3'];
arr.some(x=>console.log(x));//1 2 3
  1. map

map是一种转换方法,详细的以后再说

1
2
let arr = ['1','2','3'];
console.log(arr.map(x=>'4')); //4 4 4

将数组转换成字符串

首先是toString(),他只能将数组各个元素用逗号隔开

1
2
let arr = ['1','2','3'];
console.log(arr.toString()); //1,2,3

如果我们想用其他字符隔开各个元素,就可以使用join()

1
2
let arr = ['1','2','3'];
console.log(arr.join('|'));//1|2|3

箭头函数和扩展

发表于 2019-07-23 | 分类于 ES6

默认值

在ES6中给我们提供了可以在函数参数中使用默认值的操作,在学习箭头函数之前就先用ES5方法写函数

1
2
3
4
function func(a,b=0){
return a+b;
}
console.log(func(1)); //1

主动抛出错误

只需要一行代码 throw new Error(‘’)

1
2
3
4
5
6
7
function func(a,b=0){
if(a==0){
throw new Error('This is a error')
}
return a+b;
}
console.log(func(0)); //报错提示 This is a error

严谨模式

与ES5不同,ES6中严谨模式的代码不需要必须写在最前面,而是可以写在函数体内,但是需要注意的是当函数参数有默认值时是会报错的,下面我就来举个错误实例,然后给出报错信息,我们可以很清楚的看出是因为默认值

1
2
3
4
5
function func(a,b=0){
'use strict'
return a+b;
}
console.log(func(1));

error;

获得参数个数

当我们需要知道参数个数是,很简单,直接用length就可以知道,但是需要注意的是得到的只是必须传递的参数个数,带默认值的不包括在内

1
2
3
4
function func(a,b=0){
return a+b;
}
console.log(func.length); //1

箭头函数

箭头函数需要注意的就是如果你只想返回(return)一个值,就不需要用花括号,直接用箭头指向那个值即可,但是当代码不止一行时就需要用花括号来包住函数体,下面举个只返回个值得来看看箭头函数长什么样.还有就是箭头函数不能有new ,也就是说不能用在构造函数上

1
2
let func = (a,b)=>a+b;
console.log(func(1,1)); //2

数组知识

发表于 2019-07-21 | 更新于 2019-08-01 | 分类于 ES6

在ES6中新增了一些数组方法来简化代码量。ES6把所有对数组的操作都归结与Array.


json数组格式转换

首先来看看json格式,要注意与普通的json不一样的是必须后面要跟一个length

1
2
3
4
5
6
let json = {
'0' : 'aaa',
'1' : 'bbb',
'2' : 'ccc',
length: 3
}

下面我们只需一步就可以把它打印成数组形式,那就是调用from方法

1
2
let arr = Array.from(json);
console.log(arr); // ["aaa", "bbb", "ccc"]

Array.of()

of方法可以用于将一堆变量或字符串转化成数组的情况下。

1
2
let arr = Array.of('1','2','3');
console.log(arr); //["1", "2", "3"]

find 实例方法

find(function(value,index,arr){})三个参数缺一不可,用于寻找元素,比如

1
2
3
4
let arr = [1,2,3,4,5,6,7];
console.log(arr.find(function(value,index,arr){
return value>4; //5
}))

函数里的函数体根据需要写,当然不仅是数字,也可用来查找字符串。

fill实例方法

fill方法是用来替换数组中元素的,他有三个参数,分别是替换值,起始位置(包括),末位置(不包括),栗子如下(实例方法由实例调用而不是Array.)

1
2
let arr = [1,2,3,4,5,6,7];
console.log(arr.fill(0,0,4)); //0000567

数组遍历

在ES5中遍历数组我们采用的形式低效,ES6给我提供了高效的方式,如下所示

1
2
3
4
let arr = [1,2,3,4,5,6];
for(let value of arr){
console.log(value);
}

当我们想得到索引时可以这样

1
2
3
4
let arr = [1,2,3,4,5,6];
for(let index of arr.keys()){
console.log(index);
}

当我们需要索引和元素一起打印时可以这样

1
2
3
4
let arr = [1,2,3,4,5,6];
for(let [index,value] of arr.entries()){
console.log(index+':'+value);
}

entries()实例方法

当我们想要数组里的元素不按常规形式输出,而是想要像输出一个元素就来个一行样式

1
2
3
let arr = [1,2,3,4,5,6];
let list = arr.entries();
console.log(list); //Iterator {}

这样我们就得到了Iterator {},然后就可以用next手动循环

1
2
3
4
5
6
console.log(list.next().value);
console.log('----------------');
console.log(list.next().value);
console.log('################');
console.log(list.next().value);
console.log('----------------');

数字的操作

发表于 2019-07-20 | 分类于 ES6

ES6中将数字的方法都用Number.来开始,因为这样随着以后的完善可以扩展出很多但是又都归结于Number.下面先回顾下二进制,八进制,再介绍些可以直接用的很方便的方法

阅读全文 »

字符串操作

发表于 2019-07-20 | 分类于 ES6

字符串的操作是很重要的,但ES5的方法容易出错还比较麻烦,ES6的字符串模板,字符串查找,字符串复制就很好的解决了这些问题。

阅读全文 »

扩展运算符和rest运算符

发表于 2019-07-20 | 分类于 ES6

扩展运算符和rest运算符都是三个点…,可以当做没什么区别,但各有各的用处。

阅读全文 »

变量的解构赋值

发表于 2019-07-19 | 分类于 ES6

解构赋值就是把变量(数组,对象,字符串中的元素拿出来)->解构,再赋值,应用很多,还在初学阶段就只是把它结构出来打印下来还不会应用

阅读全文 »
1…567…12
Liu Xue

Liu Xue

119 日志
11 分类
RSS
GitHub E-Mail
© 2020 Liu Xue
由 Hexo 强力驱动 v3.8.0
|
主题 – NexT.Pisces v7.1.1
本站总访问量 次 | 有人看过我的博客啦