Javascript 不能将函数中的类用作参数(JScript)

Javascript 不能将函数中的类用作参数(JScript),javascript,Javascript,我正在尝试做一个小型JavaScript实验室。在实验室中,我首先创建了一个动物对象: function Animal(species, nature) { this.species = species; this.nature = nature; var preys = new Array(); Animal.prototype.getSpecies = function() { return species; } Animal.prototype.g

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

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;
  }
}
然后,我创建了一个世界对象,它基本上会存储一些动物对象,并根据它们的性质将它们分开

/// <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) {
  }
}
//
函数世界(){
var=newarray();
动物。推(新动物(“狼”、“食肉动物”);
动物。推(新动物(“鳄鱼”、“食肉动物”);
动物。推(新动物(“绵羊”、“杂食动物”);
World.prototype.getOmnivores=函数(){
返回动物。过滤器(getOmnivores());
}
杂食动物的功能{
}
}
在我的
getOmnivors
函数中,我不能使用
Animal
类作为变量。这对我来说有点复杂,因为我是JavaScript新手,无论它们的类型如何,我们都使用
var
关键字(或者在某些地方不使用,比如函数中的参数)

我做错了什么?我该如何修复它?在我的私人函数
getOmnivores
中,我无法访问
Animal
类。我想程序不明白这是一个叫做
Animal

我希望我解释得很好。祝你今天愉快

编辑

错误图片:

将函数作为参数

您必须提供该函数,但在代码中您立即调用该函数:

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";
  });
}
删除括号以仅提供函数而不调用它,或插入匿名函数:

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";
  });
}
将函数作为参数

您必须提供该函数,但在代码中您立即调用该函数:

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";
  });
}
删除括号以仅提供函数而不调用它,或插入匿名函数:

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";
  });
}

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

因为数组的每个元素都是类
Animal
的实例,所以可以直接使用它

此外,语法
{ClassName}.Prototype.{functionName}
不应在同一类中使用,因为当解释器到达该行时,动物类尚未定义。该语法用于已经存在和定义的类。改用
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());

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

因为数组的每个元素都是类
Animal
的实例,所以可以直接使用它

此外,语法
{ClassName}.Prototype.{functionName}
不应在同一类中使用,因为当解释器到达该行时,动物类尚未定义。该语法用于已经存在和定义的类。改用
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());

工作时,需要将函数作为参数传递,而不是它返回的内容

 return animals.filter(isOmnivore);
isOmnivore
成为

function isOmnivore(animal) {
  animal.nature == 'Omnivore';
}

您需要将函数作为参数传递,而不是它返回的内容

 return animals.filter(isOmnivore);
isOmnivore
成为

function isOmnivore(animal) {
  animal.nature == 'Omnivore';
}

显示如何尝试访问
函数getOmnivores
中的
动物
。出现了什么错误?使用构造函数函数名作为参数,这是个问题。为什么
getOmnivores
需要一个参数?实现方法是什么?@Justinas当我将
animal.
写入我的
getOmnivors
函数时,它表示提供的列表包含文件中的所有标识符。@Jeroen我将
getOmnivors
函数中的参数animal更改为animal或xxx,但没有更改。我在网上看到了使用
Array.filter()
函数的例子。在示例中,有这样的用法。以下是。
过滤器
需要一个
函数
,但您正在传递
getOmnivores()
的返回值(它可能是
未定义的
数组
)。试试这个:
returnanimals.filter(getOmnivores)显示如何尝试访问
函数getOmnivores
中的
动物
。出现了什么错误?使用构造函数函数名作为参数,这是个问题。为什么
getOmnivores
需要一个参数?实现方法是什么?@Justinas当我将
animal.
写入我的
getOmnivors
函数时,它表示提供的列表包含文件中的所有标识符。@Jeroen我将
getOmnivors
函数中的参数animal更改为animal或xxx,但没有更改。我在网上看到了使用
Array.filter()
函数的例子。在示例中,有这样的用法。以下是。
过滤器
需要一个
函数
,但您正在传递
getOmnivores()
的返回值(它可能是
未定义的
数组
)。试试这个:
returnanimals.filter(getOmnivores)非常感谢,这真的帮助了我,祝你有愉快的一天!!非常感谢,这真的帮助了我,祝你今天愉快!!谢谢,这帮了我很大的忙,祝你有愉快的一天!谢谢,这帮了我很大的忙,祝你有愉快的一天!