递归中变量的变化问题

力扣第30题中,我使用了一个递归AllComb,但是used的值出现了问题。当使用"barfoothefoobarman", ["foo","bar"]测试时,我最后得到的list只有"foobar"一个元素。我发现是因为递归在从foo bar这个分支返回的时候把bar也推入了used,但按理来说这个used返回的时候就消失了,但结束foo在前面分支,开始bar在前面分支时,那个used已经包含了bar这个元素。这是为什么呢?

//
//                               ""
//                             /    \
//            "foo"                            "bar"
//    /                 \              /                  \
//"foofoo"            "foobar"      "barfoo"          "barbar"
//在递归到第二层bar处时那次迭代的used应该只有foo,但used是["foo","bar"]
//不知道什么原因,bar只在foobar那次推入了那次递归的used,return之后那个used应该已经没了

var findSubstring = function(s, words) {
    let list = [];
    let result = [];

    function AllComb(str, num, used){
        console.log(str);
        if(num === 0){
            list.push(str);
            console.log("!!!");
            return; 
        }

        for(let i = 0; i < words.length; i++){
            console.log("used:",used);
            console.log("i=",i);
            let include = false;

            for(let j = 0; j < used.length; j++){
                if(words[i] === used[j] ){
                    console.log("include:",used[j]);
                    include = true;
                    break;
                }
            }

            if(include === true){
                console.log("跳过")
                continue;
            }
            else{
                used.push(words[i])
                console.log("--------------")
                AllComb(str+words[i],num-1,used);
            }     
        }
    }

    AllComb("", words.length,[]);
    console.log(list);

    for(let i = 0; i < list.length; i++){
        for(let j = 0; j < s.length - list[i].length; j++){
            let counter = 0;
            for(let k = 0; k < list[i].length; k++){
                if(list[i][k] === s[j+k]){
                    counter++
                }
                else{
                    counter = 0;
                }
                if(counter === list[i].length){
                    result.push(j);
                }
            }
        }
    }

    return result;
};