简体   繁体   English

使用方法导出匿名函数

[英]Export anonymous function with methods

Got a question about exporting anonymous function using Node.js What expected in result from this export: 有一个关于使用Node.js导出匿名函数的问题这个导出的结果是什么:

var date = require('./index.js'); 
var time = date('2017-05-16 13:45')
.add(24, 'hours')
.subtract(1, 'months')
.add(3, 'days')
.add(15, 'minutes');

In index.js i've tried to export anonymous function like 在index.js中,我试图导出匿名函数,如

module.exports = function(date){
    return {
        exports: function(date){},
        add: function (value, type) {},
        subtract: function (value, type) {}
    };
}

So got 2 problems with it: 所以有2个问题:

  1. It couldn't be called via date('2017-05-16 13:45') -- is it possible to define a 'constructor' of anonymous function with returned value from it? 它不能通过日期调用('2017-05-16 13:45') - 是否可以定义一个具有返回值的匿名函数的'构造函数'? How to define default behaviour when freshly imported anonymous function called by itself? 如何在自己调用新导入的匿名函数时定义默认行为?
  2. It couldn't be called in form time.add().subtract().add... 它无法以time.add()形式调用.subract()。add ...

New to js world, so any help would be very appreciated! 新的js世界,所以任何帮助将非常感谢!

Thanks in advance, Igor 谢谢你,伊戈尔

  1. This appears callable for me, which version of node are you using? 这对我来说似乎是可调用的,您使用的是哪个版本的节点? You seem to be importing your library from ./index.js - is that definitely the right file? 你似乎是从./index.js导入你的库 - 这绝对是正确的文件?

  2. You'll need to return this in order to chain methods: 您需要返回this链接方法:

test.js test.js

var date = require('./index.js'); 
var time = date('2017-05-16 13:45')
.add(24, 'hours')
.subtract(1, 'months')
.add(3, 'days')
.add(15, 'minutes');

index.js: index.js:

// NB: I've named the outer variable `_data` since you can
// access it from within the other functions (closure)
module.exports = function(_date) {

  return {
    exports: function(date) {
      // do things here

      return this;
    },
    add: function(value, type) {
      // do things here

      return this;
    },
    subtract: function(value, type) {
      // do things here

      return this;
    }
  };
}

In case you're not attached to your current approach, here are two alternatives which should work fine for your purposes: 如果您不依赖于当前的方法,这里有两个替代方案可以正常用于您的目的:

with an actual constructor function: 使用实际的构造函数:

// the benefit here is that your methods don't get recreated for every
// instance of the class

function Date(_date) {
  this.date = _date;
}

Date.prototype = Object.assign(Date.prototype, {
  exports: function(date) {
    // do things here
    console.log(date, this.date);

    return this;
  },

  add: function(value, type) {
    return this;
  },

  subtract: function(value, type) {
    return this;
  }
})

// this function isn't strictly necessary, just here to 
// maintain compatibility with your original code
module.exports = function(date) {
  return new Date(date);
}

with a regular old class : 有一个普通的老class

class Date {
  constructor(date) {
    this.date = date;
  }

  exports(date) {
    return this;
  }

  add(value, type) {
    return this;
  }

  subtract(value, type) {
    return this;
  }
}

// this function isn't strictly necessary, just here to 
// maintain compatibility with your original code
module.exports = function (date) {
  return new Date(date);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM