[英]Different Behavior in Node.js and Chrome
In Node.js (ver. 0.12.0 ) I have a class with method defined like this: 在Node.js(版本0.12.0)中,我有一个类,其方法定义如下:
In ./constructor.js
: 在
./constructor.js
:
function Pow() {}
Pow.prototype.wow = require("./wow/definition.js");
module.exports = Pow;
In ./wow/definition.js
: 在
./wow/definition.js
:
var Pow = require("../constructor.js");
function wow() {
return new Pow();
}
module.exports = wow;
In ./index.js
: 在
./index.js
:
var Pow = require("./constructor.js");
var pow = new Pow();
pow.wow();
The last line of ./index.js
throws the following error: ./index.js
的最后一行抛出以下错误:
object is not a function
at Pow.wow(./wow/definition.js:5:10)
Running the same code in Google Chrome (ver. 45.0.2454.101 64-bit ), which also uses the V8 engine but not the CommonJS require system, does not result in an error: 在Google Chrome(版本45.0.2454.101 64位)中运行相同的代码(也使用V8引擎但不使用CommonJS require系统)不会导致错误:
function Pow() {} Pow.prototype.wow = wow; function wow() { return new Pow(); } var pow = new Pow(); pow.wow();
Can anyone tell me why, in Nodejs, require("./constructor.js")
returns a constructor in ./index.js
but a non-constructable object in ./wow/definition.js
? 谁能告诉我,为什么在的NodeJS,
require("./constructor.js")
返回在构造函数中./index.js
但在非施工的对象./wow/definition.js
?
As @slebetman mentions in his comment on the original question, support for circular dependencies in CommonJS, the specification most similar to the implementation of require()
in NodeJs, is still somewhat buggy . 正如@slebetman在对原始问题的评论中提到的那样, 对CommonJS中循环依赖的支持(该规范与NodeJs中
require()
的实现最相似require()
仍然有些问题 。 So, avoid ciricular dependencies until support for them in NodeJs has been worked out. 因此,请避免在NodeJs中已经解决了对循环依赖的问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.