gpt4 book ai didi

javascript - 无法从另一个 JavaScript 文件访问 JavaScript 对象

转载 作者:行者123 更新时间:2023-11-29 21:24:36 24 4
gpt4 key购买 nike

<分区>

我正在尝试在 World 对象中使用 Animal 对象,但它们都在不同的文件中。

Animal.js :

function Animal(species, nature) {

// Public Fields
this.species = species;
this.nature = nature;

// Private Fields
var preys = new Array();

// GET Functions
..........
..........

// SET Functions
..........
..........
}

世界.js:

function World() {

var animals = new Array();

var animal = new Animal("Wolf", "Carnivore"); //[1]

animals.push(new Animal("Wolf", "Carnivore")); //[2]
}

我在一个基本网站中调用 word 类,该网站只包含创建新 World 实例的 javaScript 代码。然后在浏览器中,我按 F12,在第 1 步和第 2 步中我都遇到了错误。

错误截图:[ Error image ]

我认为它找不到动物类。我阅读了很多文章并在互联网上进行了搜索,但我找不到(可能是因为 JavaScript 是新手)。

编辑:

换句话说,我正在尝试将 Animal.js 文件导入到 World.js 文件中。然后我将导入 World.jsEcosystem.js

Folders in my project

这是 asp.net 文件夹:

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script src="scripts/World.js"></script>
<script type="text/javascript">
world = new World();
world.getOmnivores();
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
</form>
</body>
</html>

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com