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?
2 Answers 2
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']
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.Jencel– Jencel2016年03月03日 15:20:51 +00:00Commented Mar 3, 2016 at 15:20
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 }; }