3

I have been working on a project in JavaScript which requires a data structure (read only), to be shared between two functions.

var mySharedData = ['hours', 'minutes', 'seconds'];

Now I have two functions that need access to this (static) read only data structure.

var sampleFunction1 = function(userSuppliedData) {
    //map over user data applying mySharedData to it
};

var sampleFunction2 = function(userSuppliedData) {
    //reduce user data also accessing mySharedData
};

Since this is JavaScript and both functions are in the same scope I could just "cheat" and leverage the bad scoping of var and access the shared data in both functions but I don't feel like this is the proper way of doing it.

I also considered currying both functions and just passing the shared data as the first argument like so:

var mySharedData = ['hours', 'minutes', 'seconds'];

var sampleFunction1 = _.curry(function(sharedData, userSuppliedData) {
    //map over user data applying sharedData to it
})(mySharedData);

var sampleFunction2 = _.curry(function(sharedData, userSuppliedData) {
    //reduce user data also accessing sharedData
})(mySharedData);

What is the recommended way of sharing data between functions? Should this even be done in functional programming or am I making my functions impure with shared data structures?

  • The shared data would be common to both closures – Basile Starynkevitch Aug 22 '15 at 17:00
  • So you suggest doing something like this? function() { //now I'm in a closure var mySharedData = ['hours', 'minutes', 'seconds']; var sampleFunction1 = function(userSuppliedData) { //map over user data applying mySharedData to it }; var sampleFunction2 = function(userSuppliedData) { //reduce user data also accessing mySharedData }; } – KanskjeBen Aug 22 '15 at 17:42
  • Every user function is implemented as a closure in Javascript – Basile Starynkevitch Aug 22 '15 at 17:51
  • 4
    Why would you consider using the closure as cheating? – Idan Arye Aug 22 '15 at 18:41
5

If mySharedData is private for both closures:

(function (context) {
    var mySharedData = ['hours', 'minutes', 'seconds'];

    context.sampleFunction1 = function (userSuppliedData) {
        //map over user data applying mySharedData to it
        // mySharedData....
    };

    context.sampleFunction2 = function (userSuppliedData) {
        //reduce user data also accessing mySharedData
        // mySharedData....
    };
})(this);

If mySharedData is public:

var mySharedData = ['hours', 'minutes', 'seconds'];

var sampleFunction1 = function (userSuppliedData) {
    //map over user data applying mySharedData to it
    // mySharedData...
};

var sampleFunction2 = function (userSuppliedData) {
    //reduce user data also accessing mySharedData
    // mySharedData...
};

If what you need is that mySharedData can not be edited:

Object.defineProperty(this, 'mySharedData', {
    get: function () {
        return ['hours', 'minutes', 'seconds'];
    }
});

console.log(mySharedData);
// ['hours', 'minutes', 'seconds']

mySharedData.push('foo');
mySharedData[1] = 'bar';

console.log(mySharedData);
// ['hours', 'minutes', 'seconds']
-2

Use it any way you want.

Using an immutable constant from two functions is not in any way problematic or impure.

A problem arises if one of the functions mutates the value. In JS this can be prevented by declaring the binding with const and by using Object.freeze to make the value immutable.

  • I added some details, if the downvoters wish to explain their view it would be interesting for me. – Bobby Marinoff Mar 3 '16 at 15:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.