(非常)新手Javascripter需要帮助理解实际功能

时间:2017-09-26 00:59:42

标签: javascript function

所以我正在制作一个选举剧本,将候选人分配给党派和选民,然后给他们分配投票,但是如果有人可以帮助我理解我在做什么,例如这个功能在这里,我就是如此吵架:

        class Electorate    {
constructor (newNames) {
this.name = newNames
this.allMyElectorates = []
this.addWinner = []
this.addPartyVotes = []
    }
    addElectorate (...newNames) {
        for(let i=0;i<newNames.length;i++){
            var newElectorate = new Electorate(newNames[i], this.addWinner, 
            this.addPartyVotes)
            this.allMyElectorates.push(newElectorate)
            return newElectorate
    }
}
    addWinner(...newNames){
        theParty = myElection.findParty (partyName)
            if (theParty == undefined){
                myElection.addIndependant (cadidateName)
            }else{
                theCandidate = theParty.findCandidate (candidateName)
            }if (theCandidate == undefined) {
                theParty.addElectorateMP  (candidateName, this)
            }else{
                theCandidate.setElectorate (this)}
        }
    function totalVotes(...newNumbers){
        for(let i=0;i<newNumbers.length;i++){
            var newTotalVotes = new totalVotes(newNumbers[i], this)
            this.addPartyVotes.push(newTotalVotes)
            return newTotalVotes
        }
    }
    function addWinner(...newNames){
        for(let i=0;i<newNumbers.length;i++){
            var addWinner = new Winner(newNames[i], this)
            this.addWinner.push(newWinner)
            return addWinner
        }
    }


}

这就是我现在试图引用的内容:

 anElectorate = theElection.addElectorate('Auckland Central')
 anElectorate.addWinner('KAYE, Nicola Laura', 'National Party')
 anElectorate.addPartyVotes(329, 85, 10, 486, 3, 2, 6242, 553, 6101, 158, 
                            12652, 1459, 7, 17, 53, 99)

我想使用从addPartyVotes(在控制器类中)收集的数据创建一个新函数(totalVotes),它必须从其他类调用,它有它的变量,我推动它在数组中然后返回它所以我做错了什么?

我试过要求我班上的人和导师,但我觉得他们只是在没有给我任何实际指导的情况下离开我,我是工程师而不是程序员所以这很难包装我的头。

2 个答案:

答案 0 :(得分:0)

函数不能将this作为参数。

有效的功能如下所示:

function totalVotes ( vote ) {
   return "something";
}

如果您可以分享与投票/选举计划相关的整个脚本,我可以帮助指导您编写有效代码的方法。

答案 1 :(得分:0)

没有单行或代码点破坏您的程序。 有无数的错误,根本就不起作用。

看一下这个例子(JS ES5):

var Electorate = {
  candidates: [],
  init: function() {
    this.candidates = [];
    return this;
  },
  getCandidateByName: function(name) {
    var filter_candidate_by_name = this.candidates.filter(function(d) {
      return d.name === name;
    })[0];
    var index_of_candidate = this.candidates.indexOf(filter_candidate_by_name);
    return this.candidates[index_of_candidate];
  },
  calculateWinner: function() {
    var max_votes = Math.max.apply(Math, this.candidates.map(function(d) {
      return d.votes.length;
    }));
    if (!max_votes || isNaN(max_votes)) return false;
    var records_with_max_votes = this.candidates.filter(function(d) {
      return d.votes.length === max_votes;
    });
    var result = {};
    if (records_with_max_votes.length > 1) {
      result.result = 'Tied';
      result.candidates = records_with_max_votes;
      var list_of_candidate_names = records_with_max_votes.map(function(d) {
        return d.name;
      }).join(', ');
      result.explaination = 'Tied between ' + list_of_candidate_names + ', with a count of ' + max_votes + ' votes each';
    } else if (records_with_max_votes.length === 1) {
      result.result = 'Won';
      result.candidate = records_with_max_votes[0];
      result.explaination = records_with_max_votes[0].name + ' won with a count of ' + max_votes + ' votes';
    }
    return result;
  }
};

var Voter = {
  name: null,
  age: null,
  gender: null,
  init: function(name, age, gender) {
    if (!name || !age || !gender) return false;
    this.name = name;
    this.age = age;
    this.gender = gender;
    return this;
  }
};

var Candidate = {
  name: null,
  votes: [],
  init: function(name) {
    this.name = name;
    this.votes = [];
    return this;
  },
  castVote: function(voter) {
    this.votes.push(voter);
    return this;
  }
};


var electorate = Object.create(Electorate).init();

electorate.candidates.push(
  Object.create(Candidate).init('Mr. John Smith'),
  Object.create(Candidate).init('Mr. Adam John'),
  Object.create(Candidate).init('Ms. Christina Brown')
);

electorate
  .getCandidateByName('Mr. John Smith')
  .castVote(Object.create(Voter).init('Maria Smith', 38, 'Female'))
  .castVote(Object.create(Voter).init('John Doe', 118, 'Male'))
  .castVote(Object.create(Voter).init('John Doe', 44, 'Male'));

electorate
  .getCandidateByName('Ms. Christina Brown')
  .castVote(Object.create(Voter).init('John Doe', 235, 'Male'))
  .castVote(Object.create(Voter).init('John Doe', 34, 'Male'))
  .castVote(Object.create(Voter).init('John Doe', 22, 'Male'));


console.log(electorate.calculateWinner());

您可以看到它收集信息并考虑可以创建并添加到选民的适当数据位置的候选人和选民。

然后可以在所有选票投票后继续进行,并宣布所选的获胜者或并列获胜者。

我的建议是了解您的Javascript知识,并尝试不再使用ES6。

这是一个很好的资源,可用于浏览Javascript(适用于所有级别的体验):https://github.com/getify/You-Dont-Know-JS