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?

4
  • The shared data would be common to both closures Commented Aug 22, 2015 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
    Commented Aug 22, 2015 at 17:42
  • Every user function is implemented as a closure in Javascript Commented Aug 22, 2015 at 17:51
  • 4
    Why would you consider using the closure as cheating?
    – Idan Arye
    Commented Aug 22, 2015 at 18:41

2 Answers 2

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.

1
  • I added some details, if the downvoters wish to explain their view it would be interesting for me.
    – Jencel
    Commented Mar 3, 2016 at 15:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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