需要javascript(spidermonkey)和NodeJ中的差异
在javascriptspidermonkey中 //t1.js需要javascript(spidermonkey)和NodeJ中的差异,javascript,node.js,Javascript,Node.js,在javascriptspidermonkey中 //t1.js var a=function(){ }; a.prototype.dosomething=function(){ console("t1"); } t、 js 它等于: var a=function(){ }; a.prototype.dosomething=function(){ console("t1"); } var b1=new a(); b1.dosomething(); 但是在nod
var a=function(){
};
a.prototype.dosomething=function(){
console("t1");
}
t、 js
它等于:
var a=function(){
};
a.prototype.dosomething=function(){
console("t1");
}
var b1=new a();
b1.dosomething();
但是在node.js中
t、 js
它报告错误:
节点t.js
t.js:
var b1=new a();
^
ReferenceError: a is not defined
at Object.<anonymous> (/Users/mymac/js/t.js:16:12)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)
at startup (node.js:119:16)
at node.js:906:3
欢迎发表评论除非特别要求,否则NodeJS的模块系统不会泄漏全局消息 如果需要导出对它的引用,请使用module.exports: 保持全球环境清洁。在ES6中,这将由模块加载器完成
请参阅。node t.js t.js:16 var b1=new a;^ReferenceError:a未在对象上定义。我已使用t.js中的module.exports=function{this.doSomething=function{}和所需文件中的这段代码测试了我的代码,并对其进行了编译。
require ('./t1.js');
var b1=new a();
b1.dosomething();
t.js:
var b1=new a();
^
ReferenceError: a is not defined
at Object.<anonymous> (/Users/mymac/js/t.js:16:12)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)
at startup (node.js:119:16)
at node.js:906:3
//t.js
module.exports = a;
// requiring file
var A = require ('./t1.js');
var b1=new A();
b1.dosomething();