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);
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章