2017-10-10 116 views
10

在發佈另外一個問題,這是有:MS Edge爲什麼不使用spread元素和querySelector?

this.products = [...document.querySelectorAll('.product')]; 

邊緣將失敗,並出現以下錯誤:

function expected

但是這是工作:

var params = ['hello', '', 7]; 
var other = [ 1, 2, ...params]; 

爲什麼沒有頂一個在Edge上工作(它在Chrome上)?

+1

你試過'[...(document.querySelectorAll('。product'))]'? –

+1

我對您的建議做了,不工作! – Mouser

+5

Edge可能不會爲'NodeList'實現迭代器協議? *編輯:*是:https://developer.mozilla.org/en-US/docs/Web/API/NodeList#Browser_compatibility(至少它不支持'entries()','values()'等,如果它支持迭代器協議,我相信它會。)。 –

回答

7

您可以使用Array.from,它可以從像對象這樣的數組中生成一個數組。

this.products = Array.from(document.querySelectorAll('.product')); 
+2

無論如何都是用於類型轉換的[首選語法](https://stackoverflow.com/a/40549565/1048572) – Bergi

1

那麼它看起來像BERGI和Felix在正確的軌道上:在這個document上MDN他們談論的迭代器。

Some built-in constructs, such as the spread operator, use the same iteration protocol under the hood:

那麼,陣確實有entries()在邊緣一nodelist不和不支持迭代。

妮娜的答案是轉到一個!

相關問題