javascript - 为什么这个 "if"通过了,但实际上它不应该通过?

标签 javascript oop js.class

美好的一天,首先我想说的是,我并不自豪地发布整个源代码。我无法将问题隔离在较小的 block 中。

这是文件的快照:

getRoomById : function( areaId, roomId ) {
    if ( this.hAreas.get( areaId ) !== null && this.hAreas.get( areaId )[ roomId ] !== undefined )
    {
        //I can get here...
        //alert( uneval( this.hAreas.get( areaId ) ) );
        // returns: ({'000001':{areaId:"000001", roomId:"000001", Informations:{Name:"Room 1"}}, '000002':{areaId:"000001", roomId:"000002", Informations:{Name:"Room 2"}}, '000003':{areaId:"000001", roomId:"000003", Informations:{Name:"Room 3"}}})

        //alert( roomId );
        // returns: 000003

        return this.hAreas.get( areaId )[ roomId ];
        // returns: undefined
    }
},

整个文件就在那里:http://jsfiddle.net/JSWCu/2/

问题:我有一个方法可以根据包含对象的 JS.Hash 测试参数。 if 希望返回 true,但是一旦进入,我就无法获取(警报或返回)该 JS.Hash 内的对象(返回未定义)。

谢谢!非常感谢您的帮助。我也希望得到避免此类错误的提示。

编辑:这是全部代码。对不起,它真的很大。它刚刚开始变得复杂,我(目前)无法将问题隔离到更小的代码片段中:

<html>
    <head>
        <style type="text/css">

        </style>
        <script type="text/javascript">
            JSCLASS_PATH = 'http://www.poc.ca/cybrix/src';
            function appendConsole( input ) {
                document.getElementById( "console" ).innerHTML += input + "<br />";
            }
        </script>
        <script type="text/javascript" src="http://www.poc.ca/cybrix/src/loader-browser.js"></script>
        <script type="text/javascript">
            JS.require('JS.Hash', 'JS.Observable', function() {
                var AreaLists = {
                    "000001" : { "Name" : "Test Area", "Loaded" : false },
                };

                var World = new JS.Class({
                    hAreas : new JS.Hash([]),

                    getAreas : function( areaId ) {
                        if ( ! this.hAreas.get( areaId ) && AreaLists[ areaId ] !== undefined )
                        {
                            //TODO: Load from external sources

                            this.hAreas.put( areaId, {
                                "000001" : new Room( areaId, "000001", { "Name" : "Room 1" } ),
                                "000002" : new Room( areaId, "000002", { "Name" : "Room 2" } ),
                                "000003" : new Room( areaId, "000003", { "Name" : "Room 3" } ),
                            });

                            AreaLists[ areaId ].Loaded = true;

                            appendConsole( "Areas #" + areaId + " : " + AreaLists[ areaId ].Name + " Created" );
                        }

                        return this.hAreas.get( areaId );
                    },
                    getRoomById : function( areaId, roomId ) {
                        if ( this.hAreas.get( areaId ) !== null && this.hAreas.get( areaId )[ roomId ] !== undefined )
                        {
                            //I can get here...
                            //alert( uneval( this.hAreas.get( areaId ) ) );
                            // returns: ({'000001':{areaId:"000001", roomId:"000001", Informations:{Name:"Room 1"}}, '000002':{areaId:"000001", roomId:"000002", Informations:{Name:"Room 2"}}, '000003':{areaId:"000001", roomId:"000003", Informations:{Name:"Room 3"}}})

                            //alert( roomId );
                            // returns: 000003

                            return this.hAreas.get( areaId )[ roomId ];
                            // returns: undefined
                        }
                    },
                    reloadAreas : function( areaId ) {
                        //Triggered by Tick only if there is no players
                    },

                    addCharacter : function( areaId, roomId, character ) {
                        if ( this.hAreas.get( areaId ) && this.hAreas.get( areaId )[ roomId ] )
                        {
                            this.hAreas.get( areaId )[ roomId ].addCharacter( character );
                        }
                    },
                    removeCharacter : function( areaId, roomId, character ) {
                        return this.hAreas.get( areaId )[ roomId ].removeCharacter( character );
                    }
                });

                var Room = new JS.Class({
                    hDoors : new JS.Hash([]),
                    hExits : new JS.Hash([]),
                    hBodies : new JS.Hash([]),
                    hObjects : new JS.Hash([]),

                    initialize : function( areaId, roomId, Informations ) {
                        this.areaId = areaId;
                        this.roomId = roomId;
                        this.Informations = Informations;

                        //TODO: Load from external sources
                        if ( areaId == "000001" && roomId == "000003" )
                        {
                            this.hObjects.put("000001", new Objects("000001", { "Name" : "A table", "Type" : 0 }) );
                            this.hObjects.put("000002", new Objects("000002", { "Name" : "A water fountain", "Type" : 1 }) );
                        }

                        appendConsole( "Room: #" + this.areaId + "-" + this.roomId + " : " + this.Informations.Name + " Created" );
                    },

                    addCharacter : function( character ) {
                        this.hBodies.put( character.characterId , character );

                        character.onArriveRoom( this );

                        if ( ! character.Informations.Stealth )
                        {
                            //TODO: Broadcast Informations to others

                            appendConsole( character.Informations.Name + " has arrived to " + this.Informations.Name );
                        }
                    },
                    removeCharacter : function( character ) {
                        var characterId = ( typeof character == "object" ) ? character.characterId : character,
                            currentCharacter = this.hBodies.remove( characterId );

                        character.onLeaveRoom( this );

                        if ( currentCharacter !== null )
                        {
                            //TODO: Broadcast Informations to others

                            appendConsole( character.Informations.Name + " has left " + this.Informations.Name );

                            return currentCharacter;
                        }

                        return undefined;
                    },

                    onArrive : function() {

                    },

                    onLeave : function() {

                    },

                    getObjects : function( objectId, hash ) {
                        if ( this.hObjects.get( objectId ) )
                        {
                            var currentObjects = this.hObjects.get( objectId );

                            if ( hash )
                            {
                                return new JS.Hash([
                                    currentObjects.objectId, currentObjects 
                                ]);
                            }

                            return currentObjects;
                        }

                        return this.hObjects;
                    },

                    toString : function( characterId ) {

                    }
                });

                var Objects = new JS.Class({
                    objectsTypes : {
                        0 : "lies",
                        1 : "stands"
                    },

                    initialize : function( objectId, Informations ) {
                        this.objectId = objectId;
                        this.Informations = Informations;

                        appendConsole( "Object: #" + this.objectId + " : " + this.Informations.Name + " Created" );
                    },

                    toString : function() {
                        return this.Informations.Name + " " + this.objectsTypes[ this.Informations.Type ] + " here.";
                    }
                });

                var Character = new JS.Class({
                    Pet : undefined,

                    initialize : function( characterId, Informations ) {
                        this.characterId = characterId;
                        this.Informations = Informations;
                        this.areaId = this.Informations.Zone.split("-")[ 0 ];
                        this.roomId = this.Informations.Zone.split("-")[ 1 ];

                        if ( this.Informations.Pet !== undefined )
                        {
                            //TODO: Load from external sources

                            if ( this.Informations.Pet === "000001" )
                            {
                                this.Pet = new Pet( "000001", { "Name" : "Molten Panther", "Zone" : this.areaId + "-" + this.roomId, "Stealth" : false } );

                                World.addCharacter( this.Pet.getArea() , this.Pet.getRoom() , this.Pet );

                                var petRoom = World.getRoomById( this.Pet.getArea() , this.Pet.getRoom() );

                                alert( petRoom ); // = undefined ????
                            }
                        }

                        appendConsole( "Character: #" + this.characterId + " : " + this.Informations.Name + " Created" );
                    },

                    onArriveRoom : function ( currentRoom ) {

                    },
                    onLeaveRoom : function( currentRoom ) {

                    },

                    onArrive : function() {

                    },
                    onLeave : function() {

                    },

                    getRoom : function() {
                        return this.roomId + "";
                    },
                    getArea : function() {
                        return this.areaId + "";
                    },
                    getInformations : function() {
                        return this.Informations;
                    },
                    hasPet : function() {
                        return ( typeof this.Pet == "object" );
                    },
                    getPet : function() {
                        return this.Pet;
                    },

                    equals : function( character ) {
                        return ( character instanceof this.klass ) && character.Informations.Name === this.Informations.Name;
                    }
                });

                var Pet = new JS.Class( Character, {

                    initialize : function( characterId, Informations ) {
                        this.callSuper();

                        appendConsole( "Pet: " + this.Informations.Name + " Created" );
                    }
                });

                //Tests
                var World = new World();
                var AreaOne = World.getAreas( "000001" );

                var Cybrix = new Character( "000001", { "Name" : "Cybrix", "Zone" : "000001-000003", "Stealth" : false, "Pet" : "000001" } );

                if ( World.getAreas( Cybrix.getArea() ) )
                {
                    World.addCharacter( Cybrix.getArea() , Cybrix.getRoom() , Cybrix );

                    //Cybrix = World.removeCharacter( Cybrix.getArea() , Cybrix.getRoom() , Cybrix );
                }
            });
        </script>
    </head>
    <body style="margin: 0; padding: 0;">
        <div id="console" style="display: block; background-color: #000; height: 100%; color: #FFF; font-family: Lucida Console;"></div>
    </body>
</html>

最佳答案

我一直在 jsfiddle 上玩你的例子,我遇到了一些相当奇怪的行为。在您的 Character 类的 initialization 方法中,您调用相关的 World.getRoomById() 方法并将其分配给名为 的变量>宠物室

就您的代码而言,当您alert(petRoom)时,您确实会得到undefined。但是,如果您alert(petRoom.roomId),您会按预期得到000003,所以我猜测它并没有真正返回true未定义的值。如果您将 petRoom 登录到 chrome 的控制台,它会将其归类为构造函数而不是对象。我不太确定那里发生了什么,但我认为这会提供一些额外的方向。我会继续玩......

更新:问题是您重写了 Room 类的 toString() 方法并且不返回任何内容。默认情况下,警报会使用对象的 toString() 方法将其转换为字符串,因为您已经覆盖了该方法并且没有返回未定义的值。

var Room = new JS.class({
        ...
        snip
        ....
        toString: function(characterId) {

        }
});

关于javascript - 为什么这个 "if"通过了,但实际上它不应该通过?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6392641/

相关文章:

c# - 如何建模实体以在编译时强制执行数据模型约束?

c++ - 调用析构函数后访问对象

javascript - 有没有人用过 JS.Class 并喜欢它?

Javascript背景颜色切换onclick

javascript - 暂停所有其他视频,无论页面上有多少视频

javascript - 我可以在内联履行对话流编辑器中存储跨对话的计数器吗?

javascript - 这是 Kendo Ui Grid 错误吗?

oop - 如何模拟依赖于基础物质的措施