不能在函数中使用类作为参数(JScript)

时间:2016-06-14 07:42:54

标签: javascript

我正在尝试做一个小型JavaScript实验室。在实验室中,我首先创建了一个Animal对象:

function Animal(species, nature) {

  this.species = species;
  this.nature = nature;

  var preys = new Array();

  Animal.prototype.getSpecies = function() {
      return species;
  }
  Animal.prototype.getNature = function() {
      return nature;
  }
  Animal.prototype.getPreys = function () {
      return preys;
  }

  Animal.prototype.setNature = function (newNature) {
      nature = newNature;
  }
  Animal.prototype.setSpecies = function (newSpecies) {
      species = newSpecies;
  }
  Animal.prototype.setPrey = function (newPreys) {
      preys = newPreys;
  }
}

然后,我创建了一个World对象,它基本上会存储一些动物对象,并根据它们的性质将它们分开。

/// <reference path="Animal.js" />

function World() {

  var animals = new Array();

  animals.push(new Animal("Wolf", "Carnivore"));
  animals.push(new Animal("Crocodile", "Carnivore"));
  animals.push(new Animal("Sheep", "Omnivore"));

  World.prototype.getOmnivores = function () {
      return animals.filter(getOmnivores());
  }

  function getOmnivores(animal) {
  }
}

在我的getOmnivors函数中,我不能将Animal类用作变量。这对我来说有点复杂,因为我是JavaScript的新手,无论他们的类型如何,我们都使用var关键字(或者不在某些地方使用,例如函数中的参数)。

我做错了什么,我该如何解决?我无法访问私人函数Animal中的getOmnivores类。我认为程序不明白它是名为Animal

的类

我希望我解释得很好。祝你有愉快的一天!

修改

错误图片:Error picture :

3 个答案:

答案 0 :(得分:2)

Animal是类名,你不需要它。使用filter时,数组的每个元素都会自动作为该函数的第一个参数传递给回调函数。

由于数组的每个元素都是类Animal的实例,因此您可以立即使用它。

此外,语法{ClassName}.Prototype.{functionName}不应在同一个类中使用,因为在解释器到达该行时,尚未定义Animal类。该语法用于已存在和已定义的类。请改用this.{functionName}

function Animal(species, nature) {

  this.species = species;
  this.nature = nature;
  this.preys = new Array();

  this.getSpecies = function() {
      return this.species;
  }
  this.getNature = function() {
      return this.nature;
  }
  this.getPreys = function () {
      return this.preys;
  }

  this.setNature = function (newNature) {
      this.nature = newNature;
  }
  this.setSpecies = function (newSpecies) {
      this.species = newSpecies;
  }
  this.setPrey = function (newPreys) {
      this.preys = newPreys;
  }
}
function World() {
  var animals = new Array();

  animals.push(new Animal("Wolf", "Carnivore"));
  animals.push(new Animal("Crocodile", "Carnivore"));
  animals.push(new Animal("Sheep", "Omnivore"));

  this.getOmnivores = function () {
    return animals.filter(this.filterOmnivores);
  }

  this.filterOmnivores= function(animal) {
    return animal.getNature()=='Omnivore';
  }
}
myworld = new World();
console.log(myworld.getOmnivores());

https://jsfiddle.net/47dyg1q9/

的工作小提琴

答案 1 :(得分:1)

filter method将函数作为参数。

您必须提供该功能,但在您的代码中,您立即调用该功能:

World.prototype.getOmnivores = function () {
  return animals.filter(getOmnivores());
}

删除括号以仅提供函数而不调用它,或插入匿名函数:

World.prototype.getOmnivores = function () {
  return animals.filter(getOmnivores);
}
// or
World.prototype.getOmnivores = function () {
  return animals.filter(function (animal) {
    return animal.nature === "omnivore";
  });
}

答案 2 :(得分:1)

您需要将函数作为参数传递,而不是将其返回。

 return animals.filter(isOmnivore);

isOmnivore变为

function isOmnivore(animal) {
  animal.nature == 'Omnivore';
}
相关问题