The problem of variable scope

clipboard.png

function iptDom() {
    var iptValue = getIptValue();
    console.log(iptValue)
    AddUl();
    createHintContent();

}

function getIptValue() {
    // inputvalue
    return emailIpt.value;
}

function AddUl() {
    for(j = 0,len=postfixList.length; j < len; jPP) {
        let li = document.createElement("li");
        li.innerText = `${iptValue}${postfixList[j]}`;
        emailWrapper.appendChild(li);
    }
}

Why is this iptValue variable wrong and undefined? My understanding is that the iptValue variable used in the addUl () function can be obtained from the outer scope (iptDom). What is wrong with my understanding

Mar.30,2021

this is the let definition variable of the same block level scope caused by the limitation of function scope


there is a simple difference between whether the variable is defined in the body of the function in which you use the code (that is, var , if it is let and const is a block). If there is, it can be used, and if not, it won't be used. You don't need to think too much about Runtime, just look at it from the code.

in your case, you'd better define parameters for AddUl .


Hello, no matter where the function is called and no matter how it is called, its lexical scope is only determined by the location of the
when the function is declared. I suggest you define the AddUl function in iptDom, and I think the problem can be solved. Have a try.

Menu