此代码来自Jon Duckett JS教科书。我的问题是minLength
不要求声明为变量吗?
var elUsername = document.getElementById(‘username’); //get username input
var elMsg = document.getElementById(‘feedback’); //get feedback element
function checkUsername(minLength){ //declare function
if(elUsername.value.length < minLength) { //if username too short
//set the error message
elMsg.textContent = 'Username must be ' + minLength + ' characters or more’;
} else {
elMsg.innerHtml = ‘ ’;
}
}
elUsername.addEventListener(‘blue’, function(){ //when it loses focus
checkUsername(5); //pass arguments here
}, false),
答案 0 :(得分:0)
minLength
是checkUsername
函数的命名参数。您不会在该函数中重新声明它。它就像你声明了一个局部变量一样,函数调用者负责将该值发送给函数。