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

标签 node.js yeoman

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

相关文章:

node.js - passport.initialize() 中间件未用于 express 4.10 的自定义回调

javascript - 下载使用 SheetJS/XLXS 在 Node 中创建的 excel 文件 [NodeJS] [React]

node.js - 如何从 node.js 应用程序永久监听智能合约中的事件?

angularjs - 脚手架 Angular 应用程序时出错

javascript - 最新的 EmberJS 无法识别最新的 Handlebars

yeoman - bower 可以自动将 <script> 标签写入 index.html 吗?

node.js - 环回错误 : connect ECONNREFUSED 127. 0.0.1:3306 (MAMP)

css - #zoom : 1; used 时 Node.js Less 编译器解析错误

node.js - 在 Yeoman 的 server/watch/reload 任务中集成 Jade

node.js - 你如何修改 generator-angular-fullstack 图标?