我通过 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/