Javascript:forEach不适用于Uint16Array(以及Int16Array,Int16Array等)

时间:2015-05-08 14:10:30

标签: javascript foreach

如果我尝试为Uint16Array调用forEach函数并得到错误。 https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Uint16Array

例如



var test = new  Uint16Array(2);
test[0]=1;
test[1]=2;
test.forEach( function(item, i, arr) { alert (item) });




返回:

  

未捕获的TypeError:test.forEach不是函数

但是代码:



var test = [1,2];
test.forEach( function(item, i, arr) { alert (item) });




工作正常。



var test= new Array();
test[0]=1;
test[1]=2;
test.forEach( function(item, i, arr) { alert (item) });




工作也很好。

我做错了什么?

2 个答案:

答案 0 :(得分:2)

您可以使用<html> <body> <pre> <script language ='JavaScript'> document.write(" \n Welcome to the type what you see game the objective of the game is to simply answer what you see on screen by typing into the prompt box, you will press on the answer button next \n to the pictures and type and answer. Once you have moved down the page and had a go at all 5 questions, your result will be displayed once you press the result button. A correct \n answer will be worth 1 pointn an incorrect answer will be worth 0 points, best of luck!") var r = 0; function checkAnswer() { a = prompt('What animal is in the image?'); if (a == 'dog') { alert('Well Done. Correct answer'); r++; } else { alert('Sorry, incorrect, it is a dog'); } } function checkAnswer2() { b = prompt('What animal is in the image?'); if (b == 'hamster') { alert('Well Done. Correct answer'); r++; } else { alert('Sorry, incorrect, it is a hamster'); } } function checkAnswer3() { c = prompt('What animal is in the image?'); if (c == 'hedgehog') { alert('Well Done. Correct answer'); r++; } else { alert('Sorry, incorrect, it is a hedgehog'); } } function checkAnswer4() { d = prompt('What animal is in the image?'); if (d == 'turtle') { alert('Well Done. Correct answer'); r++; } else { alert('Sorry, incorrect, it is a turtle'); } } function checkAnswer5() { e = prompt('What animal is in the image?'); if (e == 'hare') { alert('Well Done. Correct answer'); r++; } else { alert('Sorry, incorrect, it is a hare'); } } function showResult() { alert('the number of questions you answered correct was ' + r + ' questions'); } </script> </pre> <p>Question 1. What is this an image of?</p> <img src="http://static1.squarespace.com/static/532abed3e4b025f227941d11/t/532ce5e3e4b0f59c2979d8c2/1395451365611/" class="image1"> <button onclick="checkAnswer()">Answer</button> <p>Question 2. What is this an image of?</p> <img src="http://www.hamsters.co.uk/hamsters_images/syrian-hamster_000008437184.jpg" class="image2"> <button onclick="checkAnswer2()">Answer</button> <p>Question 3. What is this an image of?</p> <img src="http://www.hellohedgehog.com/wp-content/uploads/2013/08/Hedgehog-7.jpg" class="image3"> <button onclick="checkAnswer3()">Answer</button> <p>Question 4. What is this an image of?</p> <img src="http://s.hswstatic.com/gif/turtle-shell-1.jpg" class="image4"> <button onclick="checkAnswer4()">Answer</button> <p>Question 5. What is this an image of?</p> <img src="http://static.guim.co.uk/sys-images/Education/Pix/pictures/2011/5/19/1305816821363/The-Irish-hare-is-under-t-007.jpg" class="image5"> <button onclick="checkAnswer5()">Answer</button> <button onclick="showResult()">Result</button> </body> </html> ,但它不在类型化数组原型上(编辑 - 至少尚未,和/或不是普遍的)。因此,您必须从常规数组中找到它并使用.forEach()

.call()

有些人更喜欢明确引用[].forEach.call(someTypedArray, function(value, index) { // your code here });

Array.prototype.forEach

答案 1 :(得分:1)

请参阅MDN TypedArray.prototype.forEach() documentation here

尚未推出 - 在浏览器兼容性部分,它声明它在Firefox 38中可用,将于2015-05-19发布。