I have a following code:
const myFunc = () => {
let var1, var2, var3;
const setVar1 = (val) => {
var1 = val
}
return {
setVar1
}
}
Can you please tell me how this pattern is called(I read about it couple of days ago and can't remember)?
As far as I understand, at the moment I will produce each time new object with setVar1 method in it and if I'll produce 1000 objects, I will get 1000 copies of setVar1(not sure, because I have only pointer to the function)?
Can/Should I refactor this code with prototypical inheritance? Can you please explain your reasoning behind your answer, it's very hard to understand this concept.
Thanks.
This is what you can do with prototypal inheritance. One of the main con is you can assign
__proto__only one time.I don't think you can access your
var1orsetVar1outside of the code ofmyFuncbecause it is scoped by the language and will create new instances of it every time you run this code.What you might be looking for is an object-oriented class approach