如何使用node.js中添加的原型方法导出对象
我在一个文件中有一个对象,希望能够要求该文件,然后根据自己的想法创建该对象的新实例,但我遇到了一个障碍。这看起来非常基本,我还缺少什么 hat.js如何使用node.js中添加的原型方法导出对象,node.js,Node.js,我在一个文件中有一个对象,希望能够要求该文件,然后根据自己的想法创建该对象的新实例,但我遇到了一个障碍。这看起来非常基本,我还缺少什么 hat.js function Hat(owner) { this.owner = owner; } Hat.prototype.tip = function() { console.log("and he (" + owner + ") tipped his hat, just like this"); } exports.Hat = Hat;
function Hat(owner) {
this.owner = owner;
}
Hat.prototype.tip = function() {
console.log("and he (" + owner + ") tipped his hat, just like this");
}
exports.Hat = Hat;
节点终端
尝试1
尝试2
所以我的hat.js实际上是
function Hat(owner) {
this.owner = owner;
}
Hat.prototype.tip = function() {
console.log("and he (" + owner + ") tipped his hat, just like this");
}
util.inherits(Hat, EventEmitter);
exports.Hat = Hat;
这是一个问题,因为在开始扩展原型之前,让继承
调用显然很重要。修复很简单,移动继承
调用几行
function Hat(owner) {
this.owner = owner;
}
util.inherits(Hat, EventEmitter);
Hat.prototype.tip = function() {
console.log("and he (" + owner + ") tipped his hat, just like this");
}
exports.Hat = Hat;
你做得不对:
var Hat = require('./hat.js').Hat;
这就是你想要的。当您执行exports.Hat=Hat
您正在导出属性为Hat
的对象(exports
)。因此,当您需要'./hat.js'
时,您将获得该对象,并且需要访问hat
属性
从节点repl:
> require('./hat.js');
{ Hat: [Function: Hat] }
> require('./hat.js').Hat;
[Function: Hat]
> var Hat = require('./hat.js').Hat;
undefined
> var mighty_duck = new Hat('Emilio');
undefined
> mighty_duck.tip();
and he (Emilio) tipped his hat, just like this
当然,这导致了一个错误,因为您在tip()
中说了owner
,而不是this.owner
,但在我更改后,它工作正常:)
如果你想就这么做
var Hat = require('./hat.js'),
hat = new Hat('Emilio');
然后将导出更改为:
module.exports = exports = Hat;
它的工作原理是:
> require('./hat.js');
[Function: Hat]
> var Hat = require('./hat.js');
undefined
> var hat = new Hat('Emilio');
undefined
> hat.tip();
and he (Emilio) tipped his hat, just like this
编辑已清理并从EventEmitter继承:
var util = require('util'),
events = require('events');
var Hat = exports.Hat = function(owner) {
events.EventEmitter.call(this);
this.owner = owner;
}
util.inherits(Hat, events.EventEmitter);
Hat.prototype.tip = function() {
console.log("and he (" + this.owner + ") tipped his hat, just like this");
}
请看这里:
您可以尝试以下方法:
function Hat(owner) {
this.owner = owner;
}
Hat.prototype.tip = function() {
console.log("and he (" + this.owner + ") tipped his hat, just like this");
}
module.exports = Hat;
然后是这样的事情:
var hat = require('./hat.js');
var mighty_duck = new hat('Emilio');
mighty_duck.tip()
谢谢,这完全正确。不幸的是,我在示例代码中遗漏了(我刚刚发现的)问题。我试图扩展EventEmitter。扩展EventEmitter不会改变导出的方式;我添加了一些从事件发射器继承的清理代码。谢谢您的回答。您的观点是正确的,扩展它不会改变您导出的方式,但是如果您不首先调用
继承
,它会杀死您的原型。奇怪的怪癖。干杯右键通过Hat.prototype=newevents.EventEmitter继承代码>或类似的设置原型,覆盖其中的任何内容。设置导出不起作用,请尝试执行此操作,并要求repl中包含该文件。你得到一个空的对象。如果要覆盖exports
中的对象,则需要执行module.exports=exports=…
@Chad Yes,没错。我已经更正了我的答案,所以它现在起作用了。谢谢
var util = require('util'),
events = require('events');
var Hat = exports.Hat = function(owner) {
events.EventEmitter.call(this);
this.owner = owner;
}
util.inherits(Hat, events.EventEmitter);
Hat.prototype.tip = function() {
console.log("and he (" + this.owner + ") tipped his hat, just like this");
}
function Hat(owner) {
this.owner = owner;
}
Hat.prototype.tip = function() {
console.log("and he (" + this.owner + ") tipped his hat, just like this");
}
module.exports = Hat;
var hat = require('./hat.js');
var mighty_duck = new hat('Emilio');
mighty_duck.tip()