简体   繁体   English

当我需要顶部的函数时,为什么我的脚本中的某些点没有定义函数?

[英]Why is a function not defined at certain points in my script when I am requiring the function at the top?

I am making a game using Javascript.我正在使用 Javascript 制作游戏。 I use webpack to bundle my modules, so at the end of each Javascript file I use module.exports .我使用 webpack 来捆绑我的模块,所以在每个 Javascript 文件的末尾我使用module.exports Here is an example:下面是一个例子:

//spaceship.js
var Spaceship = function(options) {
  this.position = options.position
  this.name = options.name
}

module.exports = Spaceship


//game.js
var Spaceship = require("./spaceship");

var Game = function() {
  this.num_spaceships = 5;
  this.spaceships = [];
  // DEBUGGER 1
  this.add_spaceships();
}

Game.prototype.add_spaceships = function() {
  // DEBUGGER 2
  for(var i = 0; i < this.num_spaceships; i++) {
    this.spaceships.push(this.randomSpaceship
  }
}

Game.prototype.randomSpaceship = function() {
  //DEBUGGER 3
}

At each of the debugging points above, if I open Chrome dev tools and type in Spaceship I get Uncaught ReferenceError: Spaceship is not defined(…)在上面的每个调试点,如果我打开 Chrome 开发工具并输入Spaceship我会得到Uncaught ReferenceError: Spaceship is not defined(…)

If I change function randomSpaceship as follows:如果我改变函数randomSpaceship如下:

Game.prototype.randomSpaceship = function() {
  //DEBUGGER 3
  var s = new Spaceship();
}

Then at DEBUGGER 3, Spaceship is now defined (if I open dev tools I get that Spaceship is a function).然后在调试器 3 中,现在定义了Spaceship (如果我打开开发工具,我会发现 Spaceship 是一个函数)。

Why does this happen?为什么会发生这种情况? I imagined it could have something to do with variable hoisting perhaps, but I am declaring and assigning the variable Spaceship at the top of the file game.js.我想它可能与变量提升有关,但我在文件 game.js 的顶部声明并分配了变量 Spaceship。

This happens because you don't use Spaceship variable inside Game and add_spaceships functions where DEBUGGER 1 and DEBUGGER 2 debugging points are located and so Chrome doesn't capture this variable in closure.发生这种情况是因为您没有在Gameadd_spaceships函数中使用Spaceship变量,调试点位于add_spaceships DEBUGGER 1add_spaceships DEBUGGER 2调试点,因此 Chrome 不会在闭包中捕获此变量。 In DEBUGGER 3 you use the variable and so it's captured in a closure and you can inspect it.DEBUGGER 3您使用变量,因此它被捕获在一个闭包中,您可以检查它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当我确定函数已定义时,为什么在错误控制台上看到“函数未定义”? - Why am I seeing 'function not defined' on my error console when I'm sure my function IS defined? 为什么 Typescript 不要求我的 function 返回某个类型? - Why isn't Typescript requiring my function to return a certain type? 当脚本标签中有 2 个函数时,为什么会出现“函数未定义”错误? - Why am I getting a "function not defined" error when there are 2 functions in the script tag? 我所在的功能未定义 - The function I am in is not defined 尽管在脚本中定义了功能,但为什么在检查时却未定义函数跳过和onclick? - Why is function skipped and onclick not defined when inspecting, although defined in the script? 当我在HTML中将onkeyup用于JS函数时,为什么控制台会说我的函数未定义? - When I use onkeyup in HTML for a JS function why does console say my function is not defined? 当我定义Foo时,为什么它不是一个函数? - Why is Foo not a function when I defined it? JavaScript,为什么会出现错误“函数add中未定义数组” - JavaScript, why am I getting the error “array is not defined in the function add” 当我从 fetch 函数发送数据时,为什么我的函数返回 false? - Why is my function returning false when I am sending data from fetch function? 为什么我的函数未定义且未加载 - Why is my function not defined and not loading
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM