当“新建"时,错过原型功能.一个实例 [英] miss prototype functions when "new" a instance
本文介绍了当“新建"时,错过原型功能.一个实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在nodejs中编写了一个模块,其中Test.js对代码进行了打击
I write a module in nodejs ,which Test.js ,code blow
function Test() {
this.key = 'value';
}
Test.prototype.foo = function () { return 'foo'; }
module.exports = Test;
然后在B.js中
var Test = require('./services/Test');
var test = new Test();
console.log(test.foo());
不幸的是,我得到了未定义的方法foo", 谁能告诉我发生了什么事?非常感谢
unfortunetly, I got "undefined method foo", anyone who can Tell me what happended? thanks so much
推荐答案
function Test() {
this.key = 'value';
}
Test.prototype.foo = function () { return 'foo'; }
module.exports = new Test();//Test;
var test = require('./services/Test');
console.log(test.foo());
您可以导出Test类的新对象.试试这个. 或者,您可以使用很棒的ES6 JavaScript.
You can export the new object of Test class. try this. Or you can use ES6 JavaScript that great.
这篇关于当“新建"时,错过原型功能.一个实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文