关于在Javascript pop()方法和推() [英] About pop() and push() in Javascript
问题描述
我真的在Javascript初学者,并尝试什么,我读出尽我所能。
但是,当涉及到 POP()
和推()
,我得到一些奇怪的结果,我很琢磨一下。
这里的code:
VAR ARR = [];
arr.push(2,3);
的console.log(ARR);的console.log(arr.pop());
的console.log(ARR);
结果是:
[2,未定义×1]
3
[2]
块引用>难道不应该是:
[2,3]
3
[2]
块引用>解决方案这是因为
的console.log
的浏览器上的异步评价。通过时间)的第一个的console.log
已显示,该项目已经走了,因为弹出的结果(
。有关准确的结果,呼
的toString()
:VAR ARR = [];
arr.push(2,3);
的console.log(arr.toString()); // 2,3 - 如预期。的console.log(arr.pop());
的console.log(ARR);I'm really a beginner in Javascript, and trying what i read as much as I can.
But when comes to
pop()
andpush()
, I get some strange results that I'm wondering about.Here's the code :
var arr = []; arr.push(2,3); console.log(arr); console.log(arr.pop()); console.log(arr);
the result is :
[2, undefined × 1]
3
[2]
Shouldn't it be :
[2, 3]
3
[2]
解决方案This is due to
console.log
's asynchronous evaluation on your browser. By the time the result of the firstconsole.log
has been displayed, the item is already gone because ofpop()
.For accurate results, call
toString()
:var arr = []; arr.push(2,3); console.log(arr.toString()); // 2,3 - as expected. console.log(arr.pop()); console.log(arr);
这篇关于关于在Javascript pop()方法和推()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!