If I understand correctly, you'd like myFunc
to create a function which will add its input x
to the value numPassed
, but allow numPassed
to be overridden at a later time using setValue
. If that's the case, your code should look like this:
function myFunc(numPassed){
var num = numPassed;
function add(x) {
return num + x;
};
add.setValue = function(newVal) {
num = newVal;
};
return add;
}
Since myFunc
returns add
, everything that you want to publicly accessible from the return value must be a property of add
.