【问题标题】:exporting module in node.js and nw.js but unable to call to constructor在 node.js 和 nw.js 中导出模块但无法调用构造函数
【发布时间】:2016-12-07 19:27:24
【问题描述】:

我正在使用 node.js 和 nw.js 创建 Web 应用程序 现在我正在导出以下模块

admin.js

module.exports = function (firstName, lastName) {
this.firstName = firstName;
this.lastName = lastName;
this.fullName = function () { 
    return this.firstName + ' ' + this.lastName;
}}

并尝试在 login.js 文件中访问它

var adm= require('./model/admin.js');
var adms=new adm("hi","wow");    
adms.fullName();

但它说 adm 不是构造函数

【问题讨论】:

    标签: javascript node.js nw.js


    【解决方案1】:

    您的代码看起来很棒。

    尝试更改您的 admin.js,但通常您的代码应该可以工作

    var adm = function (firstName, lastName) {
       this.firstName = firstName;
       this.lastName = lastName;
       this.fullName = function () {
          return this.firstName + ' ' + this.lastName;
       }
    }
    var exports = module.exports = adm
    

    【讨论】:

      猜你喜欢
      • 2021-11-10
      • 2016-08-26
      • 2015-11-12
      • 1970-01-01
      • 2011-12-27
      • 2018-06-02
      • 2017-06-26
      • 2019-10-28
      • 2018-01-28
      相关资源
      最近更新 更多