I am trying to create a class that is modified by a function that is set upon construction. The problem is, how do I get this function to modify the private fields of the class it's assigned to. I have created a simplified code to explain:
https://jsfiddle.net/9zjc0k9e/ (same code as below):
The class to be modified:
foo = function(options) {
let {func} = options; //The function we send on construction
let a = []; //The variable we are trying to modify with the function
function executer(v) {
func(v);
}
return {executer};
};
Main:
//The function we will send when constructing:
let funk = function(v) {
a.push(v); // <- this 'a' is the private member of the class we wanna modify
}
//Construct:
let bar = new foo({
func: funk
});
//Run the function we sent through the public class function assigned to that
bar.executer(1); //<-- Uncaught ReferenceError: a is not defined
The error I'm getting is: Uncaught ReferenceError: a is not defined
.
I hope I have cleared the problem enough, is there a way to get this done? Hack-ish is acceptable.