- xml - AJAX/Jquery XML 解析
- 具有多重继承的 XML 模式
- .net - 枚举序列化 Json 与 XML
- XML 简单类型、简单内容、复杂类型、复杂内容
我在 Node 中不断遇到这个问题,每当我相互调用函数时,我的应用程序就会崩溃。
我已经做了这个最小的工作示例(按照它的方式工作给了我错误):
启动模块
var module2 = require('./module2');
var data = 'data';
module2.doStuff(data);
模块 2
var module3 = require('./module3');
function doStuff(data){
// Stuff happens to 'data'
module3.takeStuff(data);
}
function doSomethingElse(data){
console.log(data);
}
module.exports = {
doStuff: doStuff,
doSomethingElse: doSomethingElse
};
模块 3
var module2 = require('./module2');
function takeStuff(data){
// Stuff happens to 'data'
module2.doSomethingElse(data); // I get the type error here
}
module.exports = {
takeStuff: takeStuff
};
我得到的错误是:
module2.doSomethingElse(data); // I get the type error here
^
TypeError: undefined is not a function
start module
在 module2
中调用函数最终调用 module3
中的函数,它又调用了 module2
中的一个函数.
正确需要所有模块,它在 module2
中找到第一个方法就好了。
这里发生了什么?当需要从附带的模块中获取功能时,如何处理此模式?
编辑
调试显示该模块存在,但除了它的原型(prototype)之外它是空的。我的问题是为什么?在 Node/JavaScript 内部,这里发生了什么?
最佳答案
这里的这个问题可以很容易地解决,同时保持你的应用程序的结构(这很好,关于循环引用)。
您只需要保留系统为您的模块提供的默认exports
对象。不要用 module.exports = {...}
改变它。
以下应该有效:
var module2 = require('./module2');
var data = 'data';
module2.doStuff(data);
var module3 = require('./module3');
exports.doStuff = function doStuff(data){
// Stuff happens to 'data'
module3.takeStuff(data);
};
exports.doSomethingElse = function doSomethingElse(data){
console.log(data);
};
var module2 = require('./module2');
exports.takeStuff = function takeStuff(data){
// Stuff happens to 'data'
module2.doSomethingElse(data); // I get the type error here
};
我将尝试从您的起点的第一行开始解释发生了什么:
module.exports = {...}
更改其导出对象module.exports = {...}
更改其导出对象这里的问题在第 3 点和第 6 点之间。在您更改对它的引用 (6) 之前,模块 3 正在接收模块 2 (3) 的导出。使用 exports.method = ...
解决了这个问题,因为 exports 对象永远不会改变。
关于javascript - '类型错误 : undefined is not a function' when jumping 'tween modules,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31783473/
在 risc-v 规范中,它说 j 是 jal 的伪代码。我对它的工作原理感到困惑,因为 j (25 位立即数)和 jal (20 位立即数)的立即值范围不同 - 还有 jalr 带 12 位立即数。
希望这是我提出问题的正确位置。为什么不能使用这两个指令递归调用子例程? 提前致谢! 最佳答案 如果您“跳转并链接”,返回地址将存储在寄存器中。如果再次“跳转链接”,返回地址就会被新的地址覆盖,破坏原来
我正在尝试在运行服务器端口的数据库和另一台服务器之间创建一个 SSH 隧道,如下所示。 MySQL:3306 Server-A:3306 我想使用 Server-A:3306作为连接到数据库的数据库
我喜欢包含键:值的“跳转字典”的概念,其中值是函数。不过,我不确定我是否应该喜欢这个概念。 我想替换一长串 if if-else if 语句。 (我应该使用 switch 语句吗?) 有没有不用eva
我是 js 的新手我正在尝试通过 Prop isActive .但我收到一个错误。你们能告诉我为什么我会收到下面的错误代码吗?我在这一行收到了错误 import React from 'react'
我一直在创建自己的 UIControl 子类以用于我的调整 iDunnoU .我已经完成了 UIControl,但展开/折叠动画除外。这个动画的问题是它在展开/折叠时会向下/向上“跳跃”,而不是像我原
我正在尝试让相机在我的 LWJGL 程序中跳跃。我尝试编写一个 if/else 语句,它会说:“当你到达这个位置时,转到默认的起始位置。”到目前为止,它还在继续飞翔。这是我的代码: if (flyUp
在过去的一个半星期里,我一直在使用 Java 和 Swing 从头开始编写游戏。到目前为止,游戏运行一切顺利,除了一件事:跳跃。我正在尝试实现抛物线跳跃,以便玩家不只是向上传送一点点。相反,我希望
我如何处理 JuMP 中的稀疏矩阵? 例如,假设我想施加以下形式的约束: A * x == 0 哪里A是一个稀疏矩阵和 x变量向量。我假设 A 的稀疏性可以被利用来使优化更快。如何在 JuMP 中利用
我有一个文本字段,您通常在其中输入数字。但是,用户必须单击键盘上的数字按钮才能进入数字屏幕。有没有办法直接跳转到键盘上的屏幕并可以输入数字? 如有任何意见和建议,我们将不胜感激。 提前致谢。 最佳答案
粘性导航栏“跳跃” 我最近制作了一个导航栏,它在滚动时粘在屏幕顶部(进入网页时它从中间开始)。我是在 Mac 上做的,一切正常。但是,当在我的 Windows 桌面上进入网页时,整个导航栏要么跳动,跳
我有一个功能类似于 w3schools 的侧边栏,而 w3schools 的侧边栏显示相同的问题。侧边栏“跳跃”并在该区域留下难看的空间,当向下滚动页面和移动 chrome 的导航栏隐藏时,它在顶部留
我正在尝试创建一组在悬停时展开和收缩的 Font-Awesome 社交媒体图标。但是,当我停止将鼠标悬停在每个图标上时会出现问题,图标中间的图像会“跳跃”而不是像图标的其余部分那样平滑地折叠。这是代码
我的垂直自动滚动似乎在某些点不规律地停止,我想要一个平滑的滚动。其次,我希望垂直滚动自动重置(几乎无限循环或暗示它永无止境而不仅仅是跳跃)。我将如何着手完成这个?到目前为止,这是我的代码:http:/
我在 YouTube 上发布了一个视频,您可以在其中看到该问题。我放慢了视频速度,这样你就能真正看到跳跃。 链接:https://www.youtube.com/watch?v=17Wftj2-MRM
我在 Drupal 8 中构建了一个站点(但我认为这是一个 CSS 问题)。 当您重新加载页面时,页面“跳转”。它也会在您第一次访问时执行此操作,但如果您按“f5”,您会看得更清楚。我尝试删除 Log
如您所见,此背景将向下动画。我拥有我想要的一切正常工作,除了在动画完成后,它会跳转,这是我不想要的。我只希望它无休止地滚动而无需跳转到它。下面的 CodePen。 关于如何在大约 10 秒后继续滚动而
更新:这基本上是我想要的,但 div 固定在底部 - http://www.wduffy.co.uk/blog/wp-content/demos/jquery-scrolling-element/ 有
我正在 Python 3.4 中使用 numpy 和矩阵构建一个神经网络草图,以学习简单的 XOR。我的符号如下: a 是神经元的事件 z 是一个神经元的输入 W 是一个权重矩阵,大小为 R^{#上一
请原谅标题措辞不佳,希望我能在这里解释这个问题。 我用 jQuery 编写了一个简单的画廊控件,其中有六个图像和两个按钮控件,允许用户循环浏览整个图像集合。我的这部分工作正常,但是每当我单击一个按钮在
我是一名优秀的程序员,十分优秀!