使用Javascript,如何在其他方法中调用方法

时间:2016-10-18 22:35:27

标签: javascript html

var hangman = {
    random: Math.floor((Math.random() * 3)),
    word_bank: ["hello", "bye", "hey"],
    select: "",
    guess: "",
    wins: 0,
    loss: 0,
    dashs: [],
    split: [],
    correct_letter: [],
    replace_dash: [],
    alphabet:['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'
],
    select_word: function() {
        this.select = this.word_bank[this.random];
    },
    dash: function() {
        for (var i = 0; i < this.select.length; i++) {
            if (this.select[i] == " ") {
                this.dashs.push("&nbsp;");
            } else {
                this.dashs.push("_");    
            }
        }
    },
    split: function() {
        this.split = this.select.split("");
    },
    correct_i: function() {
        for (var i = this.select.length - 1; i >= 0; i--) {
            if (this.split[i] == this.guess) {
                this.correct_letter.push(i)
            }
        }        
    },
    replace_dash: function(){
        for (var i =  0; i < this.correct_letter.length; i++) {
            this.dash[this.correct_letter[i]] = this.guess;
        }
    },
    board_set: function(){
        this.select_word();
        this.dash();
        this.split();
        this.correct_letter = [];
        this.replace_dash = [];
    },
    play_game: function(){
        this.board_set();
        console.log(hangman.dashs);
        console.log(hangman.split);
        var alphabet = this.alphabet;
        var gameboard = $("#game");
        gameboard.html("<p>Game Board</p>" + this.dashs.join(" "));
            document.onkeyup = function(e){
                this.guess = String.fromCharCode(e.keyCode).toLowerCase();
                if (alphabet.indexOf(this.guess) >= 0) {
                    this.correct_i();
                    this.replace_dash();
                    console.log(correct_i);
                    console.log(replace_dash);
                    gameboard = $("#game");
                    gameboard.html("<p>Game Board</p>" + this.dashs.join(" "));
                }
            }
    }
};
hangman.guess = "e";

在play_game()中我试图调用此方法中的其他方法,但它一直说我调用的方法没有定义。 我试过这样做:

hangman.correct_i();
hangman.replace_dash();

这也不起作用。

0 个答案:

没有答案