0

以下に定義されているクラス Robot があります。しかし、何らかの理由で processCommand 関数にエラーがあり、それが何であるかわかりません。これはパラメーターを取る唯一の関数なので、readCommands 関数で渡す方法に何か問題があるのではないかと思っています。

どんな助けや意見も大歓迎です!

function Robot(pos){
this.pos = pos;
this.language;

this.printCoords = function(){}

this.setLanguage = function() {}

this.setPos = function() {}

this.processCommand = function(s){

    var direction = this.pos[2];
    direction = direction.replace(" ", "");

    if( (s.match('V') && this.language.match("svenska")) ||
        (s.match('L') && this.language.match("engelska")) ){
            switch(direction){
                case "N":
                    // Look West
                    this.pos[2] = " W";
                    break;
                case "E":
                    // Look North
                    this.pos[2] = " N";
                    break;
                case "S":
                    // Look East
                    this.pos[2] = " E";
                    break;
                case "W":
                    // Look South
                    this.pos[2] = " S";
                    break;
                default:
                    break;
            }       
    }
    else if( (s.match('H') && this.language.match("svenska")) ||
             (s.match('R') && this.language.match("engelska")) ){
                switch(direction){
                case "N":
                    // Look East
                    this.pos[2] = " E";
                    break;
                case "E":
                    // Look South
                    this.pos[2] = " S";
                    break;
                case "S":
                    // Look West
                    this.pos[2] = " W";
                    break;
                case "W":
                    // Look North
                    this.pos[2] = " N";
                    break;
                default:
                    break;
            }       
    }
    else if( (s.match('G') && this.language.match("svenska")) ||
             (s.match('F') && this.language.match("engelska"))){
            switch(direction){
                case "N":
                    // Walk North
                    this.pos[1] += 1;
                    break;
                case "E":
                    // Walk East
                    this.pos[0] += 1;
                    break;
                case "S":
                    // Walk South
                    this.pos[1] -= 1;
                    break;
                case "W":
                    // Walk West
                    this.pos[0] -= 1;
                    break;
                default:
                    break;
            }           
    }
    this.printCoords();
}

this.readCommands = function() {
    document.getElementById("textField").onkeyup = function(key){

        key = window.event;
        keyCode = key.keyCode || key.which;
        if(keyCode == '13'){
            var commandString = document.getElementById("textField").value;

            for(var i = 0; i < commandString.length; i++){
                this.processCommand(commandString[i]);
            }
        }
    }
}

}
4

3 に答える 3

0

あなたのイベント ハンドラー ( function(key){}) は、あなたがやりたいことをしていないと思います。

これは、イベント ハンドラー呼び出しのコンテキストでは、this通常、 のインスタンスではなく、イベントが発生した DOM 要素を参照するためですRobot

代わりに次のコードを試してください。

this.readCommands = function() {
    var that = this;

    document.getElementById("textField").onkeyup = function(key){

        key = window.event;
        keyCode = key.keyCode || key.which;
        if(keyCode == '13'){
            var commandString = document.getElementById("textField").value;

            for(var i = 0; i < commandString.length; i++){
                that.processCommand(commandString[i]);
            }
        }
    }
}

このようにして、メソッドを呼び出す実際のオブジェクトへの参照を保存processCommand()し、イベント ハンドラーで呼び出すことができます。

于 2013-09-09T19:00:54.657 に答える
-1

古い that=this トリックを使用する:

function Robot(pos){
this.pos = pos;
this.language;

this.printCoords = function(){}

this.setLanguage = function() {}

this.setPos = function() {}


this.processCommand = function(s){

    var direction = this.pos[2];
    direction = direction.replace(" ", "");

    if( (s.match('V') && this.language.match("svenska")) ||
        (s.match('L') && this.language.match("engelska")) ){
            switch(direction){
                case "N":
                    // Look West
                    this.pos[2] = " W";
                    break;
                case "E":
                    // Look North
                    this.pos[2] = " N";
                    break;
                case "S":
                    // Look East
                    this.pos[2] = " E";
                    break;
                case "W":
                    // Look South
                    this.pos[2] = " S";
                    break;
                default:
                    break;
            }       
    }
    else if( (s.match('H') && this.language.match("svenska")) ||
             (s.match('R') && this.language.match("engelska")) ){
                switch(direction){
                case "N":
                    // Look East
                    this.pos[2] = " E";
                    break;
                case "E":
                    // Look South
                    this.pos[2] = " S";
                    break;
                case "S":
                    // Look West
                    this.pos[2] = " W";
                    break;
                case "W":
                    // Look North
                    this.pos[2] = " N";
                    break;
                default:
                    break;
            }       
    }
    else if( (s.match('G') && this.language.match("svenska")) ||
             (s.match('F') && this.language.match("engelska"))){
            switch(direction){
                case "N":
                    // Walk North
                    this.pos[1] += 1;
                    break;
                case "E":
                    // Walk East
                    this.pos[0] += 1;
                    break;
                case "S":
                    // Walk South
                    this.pos[1] -= 1;
                    break;
                case "W":
                    // Walk West
                    this.pos[0] -= 1;
                    break;
                default:
                    break;
            }           
    }
    this.printCoords();
}

var that=this;

this.readCommands = function() {
    document.getElementById("textField").onkeyup = function(key){

        key = window.event;
        keyCode = key.keyCode || key.which;
        if(keyCode == '13'){
            var commandString = document.getElementById("textField").value;

            for(var i = 0; i < commandString.length; i++){
                that.processCommand(commandString[i]);
            }
        }
    }
}

}
于 2013-09-09T19:00:34.113 に答える
-1

これは、すぐに見て答えを推測するための大量のコードです。ただし、thisエラーは通常、this必要と思われる値を持っていないことが原因です。

thisの値は、関数の呼び出しコンテキストに基づいて変化することを認識することが重要です。このようなエラーを解決する典型的な解決策は、this後で参照できる変数に の値を格納することです。

var self = this;

self直接の代わりに使用thisします。

于 2013-09-09T19:01:08.117 に答える