admin管理员组

文章数量:1317915

Got a question about exporting anonymous function using Node.js What expected in result from this export:

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

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

So got 2 problems with it:

  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? 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...

New to js world, so any help would be very appreciated!

Thanks in advance, Igor

Got a question about exporting anonymous function using Node.js What expected in result from this export:

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

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

So got 2 problems with it:

  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? 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...

New to js world, so any help would be very appreciated!

Thanks in advance, Igor

Share Improve this question asked Feb 6, 2018 at 16:04 silendsilend 431 gold badge1 silver badge6 bronze badges 2
  • re 1) can you clarify what you mean by "It couldn't be called via date('2017-05-16 13:45')" because I've tested it and it can be called with that string. – Andy Commented Feb 6, 2018 at 16:22
  • Is it necessary for it to be an anonymous function? It would require more code to get that working the way you want rather than just using a class and wrapping it – casraf Commented Feb 6, 2018 at 16:28
Add a ment  | 

1 Answer 1

Reset to default 5
  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?

  2. You'll need to return this in order to chain methods:

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:

// 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 patibility with your original code
module.exports = function(date) {
  return new Date(date);
}

with a regular old 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 patibility with your original code
module.exports = function (date) {
  return new Date(date);
}

本文标签: javascriptExport anonymous function with methodsStack Overflow