将函数参数插入嵌套对象链中?

时间:2018-10-09 09:45:50

标签: javascript function object parameters parameter-passing

我需要一些帮助来弄清楚如何将'any'参数插入到函数内部的对象链中。我遇到的问题是,即使将'any'参数放置在对象链中,对象方法仍然是未定义的,因为它没有将稍后在函数中设置的变量传递到链中。有什么办法可以解决这个问题?

var gameObj = {
    sounds: {
        win: document.getElementById("win-sound"),
        lose: document.getElementById("lose-sound")
    },
    elements: {
        wordGuess: document.getElementById("word-guess"),
        guesses: document.getElementById("number-guess"),
        winsDoc: document.getElementById("wins"),
        losesDoc: document.getElementById("loses"),
        letterDisplay: document.getElementById("letter-guess")
    },
    score: {
        win: 0,
        lose: 0,
        letterGuess: 6
    },
    array: {
        guessArray: [],
        guessedLetters: []
    },
    string: {
        win: "YOU WIN!",
        lose: "You lose"
    }

}

var string = gameObj.string;
var array = gameObj.array;
var score = gameObj.score;
var sound = gameObj.sounds;
var element = gameObj.elements;

//later on
function final(any) {
//I want to put ^ parameter into        this object v
            element.wordGuess.textContent = string.any;
            sound.any.play();
            score.any++
            update();
            setTimeout(function () {
                reset();
            }, 2000);
            console.log(final())
        }

        if (word === array.guessArray.join("")) {
            final(win);
        } else if (score.letterGuess === 0) {
            final(lose);
        };

0 个答案:

没有答案