HTML
<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/2.0.2/jquery.min.js"></script>
<meta charset=utf-8 />
<title>JS Bin</title>
</head>
<body>
<input type='button' name='test' id='test' value='Click'/>
</body>
</html>
Javascript
$(document).ready(function(){
$('#test').on('click',function(){
var a = 10;
testFunction(a);
});
});
function testFunction(a,b){
alert(a);
}
这里,当我单击“Click”按钮时,它会警告 10。但是在函数调用中,我只有一个参数,而在函数定义中,我有两个参数。那么为什么它会警告该值,而不是产生有关参数数量的任何错误.?
最佳答案
这是因为在 Javascript 中,参数是可选的。
它们有一个默认值,因此在伪代码中它将是
function testFunction(a = undefined,b = undefined){
alert(a);
}
关于Javascript函数定义和函数调用区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17669044/