gpt4 book ai didi

node.js - 在 Yeoman 中使用 Node.js 的 module.exports 会产生不同的结果

转载 作者:太空宇宙 更新时间:2023-11-03 22:14:34 24 4
gpt4 key购买 nike

我通过 module.exports 收到了不同的结果,并且希望有人帮助我弥补我知识中的明显漏洞。

通过以下代码,我收到下面列出的结果。

var generators = require('yeoman-generator');

var MyBase = generators.Base.extend({
helper: function() {
console.log('this is a helper method');
}
});

module.exports = MyBase.extend({
method1: function() {
console.log('method 1 just ran');
}
});

结果:

method 1 just ran

但是如果我将 module.exports 放在自己的行上,并将 MyBase 分配给它,我会得到以下结果。这是代码:

var generators = require('yeoman-generator');

var MyBase = generators.Base.extend({
helper: function() {
console.log('this is a helper method');
}
});

MyBase.extend({
method1: function() {
console.log('method 1 just ran');
}
});

module.exports = MyBase

结果:

this is a helper method

是什么导致了输出的差异?

最佳答案

我无法完全重现您的问题,但问题几乎可以肯定是在生成器上调用 .extend 返回一个具有当前属性和扩展属性的新生成器。

var generators = require('yeoman-generator');

var MyBase = generators.Base.extend({
helper: function() {
console.log('this is a helper method');
}
});

// Capture the output of the .extend function
var MyBase2 = MyBase.extend({
method1: function() {
console.log('method 1 just ran');
}
});

module.exports = MyBase2

或者,您可以一次性定义多个属性

var MyBase = generators.Base.extend({
helper: function() {
console.log('this is a helper method');
},
method1: function() {
console.log('method 1 just ran');
}
});

关于node.js - 在 Yeoman 中使用 Node.js 的 module.exports 会产生不同的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33266939/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com