WEB前端2020年更新实用代码段(持续更新)

1、使用解构获取json数据

let jsonData = {
id: 1,
status: "OK",
data: [a, b]
};
let { id, status, data: number } = jsonData;
console.log(id, status, number ); // 1,“OK”,[a, b]

2、使用扩展字符串合并数组

let a1 = [1,2];
let a2 = [3,4];
console.log([...a1,...a2]) // [1,2,3,4]

3、使用Set实现数组去重

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

4、使用apply改变this指向

let name = "maomin";
let obj = {
  name:'xqm',
  say:function(year,place){
  	console.log(this.name+' is '+year+' born from '+place);
  }
};
let say = obj.say;
setTimeout(function(){
	say.apply(obj,['1996','China']);
},0)

5、使用解构快速交换变量值

let a = 10;
let b = 20;
[a,b] = [b,a];

6、使用解构实现多变量赋值

let [a,b,c]=[1,2,3];
发布了163 篇原创文章 · 获赞 288 · 访问量 179万+

猜你喜欢

转载自blog.csdn.net/qq_39045645/article/details/104089659