在Yeoman中使用Node.js的module.exports不同的结果
我收到与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'); } });