Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/backbone.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在我的观念中,这是正确的思维方式吗?_Javascript_Node.js_Prototype_Javascript Objects - Fatal编程技术网

Javascript 在我的观念中,这是正确的思维方式吗?

Javascript 在我的观念中,这是正确的思维方式吗?,javascript,node.js,prototype,javascript-objects,Javascript,Node.js,Prototype,Javascript Objects,我试图在Node.js中编写一个高级应用程序,但我不确定我在代码中的思维方式是否正确。首先,我想谈谈我的申请。我的应用程序将在不刷新页面的情况下在线显示结果。首先,我必须创建一个团队阵列,这些团队将进行比赛。假设我们有两个这样的匹配项: var Schedule = [ {id:199, team1:{name:"Detroit",id:24319}, team2: {name: "San Antonio", id:32543}, date: "20.11.2015 12:00"},

我试图在Node.js中编写一个高级应用程序,但我不确定我在代码中的思维方式是否正确。首先,我想谈谈我的申请。我的应用程序将在不刷新页面的情况下在线显示结果。首先,我必须创建一个团队阵列,这些团队将进行比赛。假设我们有两个这样的匹配项:

var Schedule = [ {id:199, team1:{name:"Detroit",id:24319}, team2: {name: "San Antonio", id:32543}, date: "20.11.2015 12:00"},
                 {id: 200, team1: {name: "Boston", id: 23113}, team2: {name: "Los Angeles", id:25966}, date: "19.12.2015 10:00"} ];
function Match(team1,team2,id) {
    this.team1.name = team1;
    this.team2.name = team2;
    this.id = id;
    this.initialize();
};

Match.prototype =  {
    id: 0,
    minute: 0,
    team1: {"name": ""},
    team2: {"name": ""},
    result1: 0,
    result2: 0,
    events: [],

    initialize: function() {
        var that = this;

        setInterval(function(){
            that.minute++;

        Matches.forEach(function(a,b,theArray) {

            if(a.id == that.id) {
                theArray[b].minute = that.minute;
            if(that.minute == 5)
                that.score(theArray[b].team1,3);
            if(that.minute == 10)
                that.score(theArray[b].team1,2);
            if(that.minute == 20)
                that.score(theArray[b].team1,3);
            if(that.minute > 90)
                return;
                }
        });

        },1000);

    },
    score: function(team,points) {
        var that = this;
        Matches.forEach(function(a,b,theArray) {

            if(a.id == that.id) {

                if(team.name == theArray[b].team1){
                    theArray[b].result1 = theArray[b].result1 + points;

                }
                if(team.name == theArray[b].team2)
                    theArray[b].result2 = theArray[b].result2 + points;
                var message = that.getAction('score',team,points);

                that.events.push(message);
            }
        });
    }
}
其次,当匹配开始时,我必须使用创建对象开始匹配。我放入数组的这个匹配的属性:“Matches”

我的火柴工厂是这样的:

var Schedule = [ {id:199, team1:{name:"Detroit",id:24319}, team2: {name: "San Antonio", id:32543}, date: "20.11.2015 12:00"},
                 {id: 200, team1: {name: "Boston", id: 23113}, team2: {name: "Los Angeles", id:25966}, date: "19.12.2015 10:00"} ];
function Match(team1,team2,id) {
    this.team1.name = team1;
    this.team2.name = team2;
    this.id = id;
    this.initialize();
};

Match.prototype =  {
    id: 0,
    minute: 0,
    team1: {"name": ""},
    team2: {"name": ""},
    result1: 0,
    result2: 0,
    events: [],

    initialize: function() {
        var that = this;

        setInterval(function(){
            that.minute++;

        Matches.forEach(function(a,b,theArray) {

            if(a.id == that.id) {
                theArray[b].minute = that.minute;
            if(that.minute == 5)
                that.score(theArray[b].team1,3);
            if(that.minute == 10)
                that.score(theArray[b].team1,2);
            if(that.minute == 20)
                that.score(theArray[b].team1,3);
            if(that.minute > 90)
                return;
                }
        });

        },1000);

    },
    score: function(team,points) {
        var that = this;
        Matches.forEach(function(a,b,theArray) {

            if(a.id == that.id) {

                if(team.name == theArray[b].team1){
                    theArray[b].result1 = theArray[b].result1 + points;

                }
                if(team.name == theArray[b].team2)
                    theArray[b].result2 = theArray[b].result2 + points;
                var message = that.getAction('score',team,points);

                that.events.push(message);
            }
        });
    }
}
现在,我将数据发送到结果页面:

setInterval(function() {
    io.sockets.emit('results', Matches);
},1000);
现在,我的代码有一个问题,因为每个方法都需要forEach函数,在我看来,获取匹配数的方法是错误的,然后在这个匹配中进行更改。此命令的每次执行:新匹配(a.team1、a.team2、a.id);应该是单独的对象,例如,方法分数应该只更改一个匹配项作为数组中的引用


我有个问题,这是正确的方法吗?也许你有什么想法来改进我的代码?感谢您的每一个回复。

+1感谢您尝试理解高级javascript概念,希望正确使用它们,并向社区征求反馈。然而,这个问题可能需要迁移到codereview.stackexchange.com上。是的,这个问题也非常固执己见。事实上,我很惊讶地看到这个问题还没有结束。我在15分钟内就解决了很多自以为是的问题。。。你甚至为此得到了更多的选票P