ES6对于对象、函数、数组的扩展

对象的扩展

  • 属性简写(属性名和变量名一致时才可以简写)
let name = 'tom';
let age = 18;
let gender = 'male';
let obj = {name,age,gender};
  • 方法简写(只有在对象中的方法才可简写)
let obj2 = {
	sayHi(){
		console.log('hi');
	}
}
  • 属性名表达式
let name = 'tom';
let obj3 ={
	['name'+name]:'hello'
}
  • 方法的name属性
const person = { sayName() { console.log('test') } };
console.log(person.sayName.name);//'test'
console.log(typeof person.sayName.name);//string
  • Object.is(v1,v2) 判断两个值是否全等
//与===的区别 +0和-0是false,NaN是true
console.log(+0===-0);//true
console.log(NaN===NaN);//false
console.log(Object.is(+0,-0));//false
console.log(Object.is(NaN,NaN));//true
  • Object.assign(target,o1,o2...) 用于对象的合并,属于浅复制
    返回目标对象
let obj2 = Object.assign(obj);
console.log(obj2===obj);//true
let obj4 = Object.assign({},obj);
console.log(obj4===obj);//false
  • __proto__本质上属于内部属性,指向当前对象的prototype对象
    ES6中不支持使用

  • Object.getPrototypeOf(obj) 读取一个实例对象的原型对象

  • Object.setPrototypeOf(obj,newPrototype) 设置一个实例的原型对象
    返回参数对象本身
    虽然设置了新的原型对象,但是实例依旧可调用Object的属性和方法

  • Object.keys(obj) 返回属性名组成的数组

  • Object.values(obj) 返回属性值组成的数组

  • Object.entries(obj) 返回键值对组成的二维数组

for(let [key,value] of Object.entries(obj)){
	console.log(key,value);
}

函数的扩展

  • 函数参数的默认值
    函数的length属性,将返回没有指定默认值的参数的个数,遇到有默认值的参数就会停止
function test(a,b,c='1',d){}
console.log(test.length);//2
  • 箭头函数
  1. 箭头函数参数部分使用一个圆括号代表参数部分
  2. 箭头函数的代码块部分多余一条语句,就使用{}包裹,并且使用return语句返回

不能作为构造函数
有arguments属性,但是不保存实参

  • 箭头函数中的this指向
    箭头函数没有自己的this,其this指向的是箭头函数声明时的父作用域中的this
//模块的导出对象
console.log(this);//module.exports --> {} 
console.log(this==module.exports);// true
let test = ()=>{
	console.log(this);
}
test();//{}

let obj = {
	test
}
obj.test();//{}
function test(){
	console.log(this);
	return ()=>{
		console.log(this);
	}
}
let obj = {
	test:test()
}
obj.test();//golbal 两次

箭头函数的this指向即当前该箭头函数声明时父作用域中this是什么它就是什么


数组的扩展

  • Array.from(arr) 可将类数组、可遍历的对象转换为数组

只要是部署了Iterator接口的数据结构,Array.from()都能将其转换为数组

let temp = {
	'0':'hello',
	'1':'world',
	length:2
}
//没有length不成功
console.log(Array.from(temp));//[ 'hello', 'world' ]
  • Array.of(nums) 用于将一个或一组值转换为数组
console.log(Array.of(10));//[ 10 ]
console.log(Array.of(10,11));//[ 10, 11 ]

Array.of()方法的主要目的是为了弥补构造函数Array的不足。因为参数个数的不同,会导致Array的行为有差异

  • find()findIndex() 属于数组的迭代方法
    find()用于找出第一个符合条件的数组元素,若没有符合条件的,返回undefined
    findIndex()返回第一个符合条件的数组元素的索引,若没有,返回-1
let arr = [1,20,8,12,45];
let result = find((item)=>{
	return item>20;
});
let result2 = findIndex((item)=>{
	return item>20;
});
console.log(result);//45
console.log(result2);//4
  • fill 填充数组
let arr = [1,2,3];
console.log(arr.fill(9));//[ 9, 9, 9 ]
console.log(arr);//[ 9, 9, 9 ]
console.log(new Array(3).fill(7));//[ 7, 7, 7]
  • includes 判断数组中是否包含某个值,返回布尔值
let arr = [1,2,3];
console.log(arr.includes(2));//true
  • 数组实例的keys、values、entries
    keys()用来遍历数组中的索引,values()用来遍历数组中的值,entries用来遍历index-item键值对
    返回迭代器Iterator对象
let arr = [1,2,3];
for(let key of arr.keys()){
	console.log(key);//0 1 2
}
for(let value of arr.values()){
	console.log(value);//1 2 3
}
for(let [index,item] of arr.entries()){
	console.log(index,item);//0 1  1 2  2 3
}

  • for-of 遍历数组、类数组
    for-of 不能直接遍历对象,遍历对象可直接用for-in,或者for-of遍历entries
let arr = ['hello','time','reduce'];
for(let item of arr){
	console.log(item);
}
function test(){
	for(let item of arguments){
		console.log(item);
	}
}
test(1,2,3);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

King_960725

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值