0

Trying to create npm module based on JavaScript function composition by chaining

This works!

app.js

// -- runtime --
Object.defineProperties(window,
{
  world:
  {
    set: function(w)
    {
      return w();
    }
  }
});


var pureworld = require('./app');

var log = pureworld.log;
//var world = pureworld.world;

world = (log("1"))(log("2"))(log("3"))(log("4"));
//1
//2
//3
//4

However,

I try to Object.defineProperties to module.exports in nodejs

app.js

var myModule = {
  foo: foo,
  bar: bar
};

Object.defineProperties(myModule,
{
  world:
  {
    set: function(w)
    {
      return w();
    }
  }
});

module.exports = myModule;

This fails.

var pureworld = require('./app');

var log = pureworld.log;
var world = pureworld.world;

world = (log("1"))(log("2"))(log("3"))(log("4"));

Any idea what I miss? Thanks!

Community
  • 1
  • 1
  • 1
    Hrm, what exactly are you trying to do in this particular instance? What exactly isn't working? –  Jan 16 '15 at 15:06
  • 1
    Define "*not working*"? – James Jan 16 '15 at 15:09
  • Thanks guys, I edited my question for a specific usage. Basically push some value and needs to react for it. –  Jan 16 '15 at 15:12
  • 2
    You have defined no getter for `.world`, but are accessing it? What do you expect? Also, the setter is not called for variables like `world` that are no properties of your object… – Bergi Jan 16 '15 at 15:15
  • without getter this have been working. Previously this binds to window global object, but once I make this to node module, it stop working that puzzles me. –  Jan 16 '15 at 15:18
  • Sorry, my question is not clear enough I admit. –  Jan 16 '15 at 15:19
  • 1
    It was probably working because you weren't trying to get `world` before setting it - if you remove the `var world = pureworld.world` and just set `pureworld.world` directly then it should work. – James Jan 16 '15 at 15:27
  • wow, your suggestion seems correct. It start working and I must investigate how to do this better. Thanks a lot. –  Jan 16 '15 at 15:32
  • I tried, but can't. Is there any workaround for this other than just set `pureworld.world` directly??? –  Jan 16 '15 at 20:09

0 Answers0