1、sort
const arr1 = ['a', 'b', 'c'];
const arr2 = ['b', 'c', 'a'];
console.log(
arr1.sort() === arr1,
arr2.sort() == arr2,
arr1.sort() === arr2.sort()
);
Answer: true, true, false
首先,array sort
方法對原始數組進行排序,並返回對該數組的引用。這意味着在編寫arr2.sort()
時,arr2
數組對象將被排序。
然而,在比較對象時,數組的排序順序並不重要。因爲 arr1.sort()
和 arr1
指向內存中的同一個對象,所以第一個相等比較時返回 true
。對於第二個比較也是這樣:arr2.sort()
和 arr2
指向內存中的同一個對象。
在第三個比較中,arr1.sort()
和 arr2.sort()
的排序順序相同;但是,它們指向內存中的不同對象。因此,第三個測試結果爲 false
。
2、Set對象
const mySet = new Set([{ a: 1 }, { a: 1 }]);
const result = [...mySet];
console.log(result);
Answer: [{a: 1}, {a: 1}]
雖然 Set 對象確實會刪除重複項,但我們使用的兩個值是對內存中不同對象的引用,儘管它們具有相同的鍵值對。這與 {a:1}=={a:1}
爲 false
的原因相同。
應該注意的是,如果集合是使用對象變量(比如 obj={a:1}
)創建的,那麼新集合([obj,obj]
)將只有一個元素,因爲數組中的兩個元素引用內存中的同一個對象。
3、Object.freeze
const user = {
name: 'Joe',
age: 25,
pet: {
type: 'dog',
name: 'Buttercup'
}
};
Object.freeze(user);
user.pet.name = 'Daffodil';
console.log(user.pet.name);
Answer: Daffodil
Object.freeze 對象凍結將對對象執行淺凍結,但不會保護深層屬性修改不受影響。在這個例子中,我們無法修改 user.age
,但我們可以修改 user.pet.name
. 如果我們覺得需要保護對象不被“修改,我們可以遞歸地使用對象凍結或者使用現有的“深度凍結”庫。
4、Prototype
function Dog(name) {
this.name = name;
this.speak = function() {
return 'woof';
};
}
const dog = new Dog('Pogo');
Dog.prototype.speak = function() {
return 'arf';
};
console.log(dog.speak());
Answer: woof
每次創建一個新的 Dog 實例時,我們都將該實例的 speak
屬性設置爲返回字符串 woof
的函數。因爲每次我們創建一個新的 Dog 實例時都會設置這個屬性,所以解釋器永遠不必在原型鏈上查找更遠的內容來找到 speak
屬性。
5、Promise.all
const timer = a => {
return new Promise(res =>
setTimeout(() => {
res(a);
}, Math.random() * 100)
);
};
const all = Promise.all([
timer('first'),
timer('second')
]).then(data => console.log(data));
Answer: ["first", "second"]
我們可以可靠地以數組參數中提供它們的相同順序返回。
6、Reduce
const arr = [
x => x * 1,
x => x * 2,
x => x * 3,
x => x * 4
];
console.log(arr.reduce((agg, el) => agg + el(agg), 1));
Answer: 120
對於Array#reduce,聚合的初始值(這裏稱爲 agg
)在第二個參數中給出。在這種情況下,是1。然後,我們可以對函數進行迭代,如下所示:
1 + 1 * 1 = 2
2 + 2 * 2 = 6
6 + 6 * 3 = 24
24 + 24 * 4 = 120
7、擴展運算符
const arr1 = [{ firstName: 'James' }];
const arr2 = [...arr1];
arr2[0].firstName = 'Jonah';
console.log(arr1);
Answer: [{ firstName: "Jonah" }]
擴展運算符創建數組的淺拷貝,這意味着 arr2
中包含的對象仍指向 arr1
對象所指向的內存中的同一對象。因此,更改一個數組中對象的 firstName
屬性也會被另一個數組中的對象所反映。
8、Array
const map = ['a', 'b', 'c'].map.bind([1, 2, 3]);
map(el => console.log(el));
Answer: 1 2 3
['a','b','c'].map
調用時,this 指向的是['a', 'b', 'c']
,當通過 bind 方法修改 this 之後指向時變成了[1, 2, 3]
,所以代碼可以改寫成這種方式:
[1, 2, 3].map(el => console.log(el))
好了,今天的文章分享到這裏,歡迎大家點贊留言和收藏。