[英]Function call not displaying undefined value when an array is passed as param
I have created the following function in JavaScript which is working as expected:我在 JavaScript 中创建了以下函数,它按预期工作:
<script>
function add(a, b, c) {
document.write('A: ' + a + '<br>');
document.write('B: ' + b + '<br>');
document.write('C: ' + c + '<br>');
}
add(10, 20);
</script>
A: 10
B: 20
C: undefined
However, the following function in JavaScript is not working as expected wherein it is not displaying undefined
value但是,JavaScript 中的以下函数未按预期工作,其中未显示undefined
值
<script>
function addArray(a = [101]) {
document.write('A: ' + a[0] + '<br>');
document.write('B: ' + a + '<br>');
document.write('C: ' + c + '<br>');
}
addArray([110]);
</script>
A: 110
B: 110
As per my understanding the output should be as below:根据我的理解,输出应如下所示:
A: 110
B: 110
C: undefined
Also, what is difference between a[0]
and a
in the above example?另外,上面例子中的a[0]
和a
什么区别?
c is never declared c 从未被声明
function addArray(a=[101],c) { document.write('A: ' + a[0] + '<br>'); document.write('B: ' + a + '<br>'); document.write('C: ' + c + '<br>'); } addArray([110,120]);
The difference between a[0] and a is that a[0] is displaying the first index value of an
array and a is displaying all the values of the array.
Imagine A=[100,200]
document.write('A: ' + a[0] + '<br>');
Would write 100
document.write('A: ' + a + '<br>');
Would write 100,200
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.