-1

Lets say I have this function, I am trying to get values of value1 and 2 , what’s the right way to do it. 1. how do I make value 1 and value 2 defined , when it exits the function 2. how do i get the value in another function without calling “doSomething” function

test.prototype.doSomething = function (){
    test = new Execution(experiment);

    function experiment(bla) {
        forEach(function(bla) {
            var value1 = bla.value1;
            var value2 = bla.balue2;
            console.log(value1); //defined 
        });
        console.log(value1); //undefined 
    }
    console.log(value1); //undefined
}

test.prototype.testSomething = function() {
    var testSomething = values1;

}
3
  • This looks like an XY Problem. The way you have those values defined the answer is: "you don't". The question is why are you doing things in this way? What are you really trying to do? Commented Feb 20, 2018 at 22:35
  • You don't. These aren't "private" variables, they're just scoped, and they're marked for garbage collection as soon as the innermost function exits, since they're no longer in scope after that. Commented Feb 20, 2018 at 22:35
  • What are Experiment, forEach and test? Why does doSomething overwrite test? How are testSometing and doSomething called in the first place? Where do you want to get which particular value? Commented Feb 20, 2018 at 22:44

1 Answer 1

0

You can try by declaring value1 and value2 out of experiment function and assign value to those variable within the function. Below is code:

test.prototype.doSomething = function (){
var value1, value2;
test = new Execution(experiment);
function experiment(bla) {
    forEach(function(bla) 
        value1 = bla.value1;
        value2 = bla.balue2;
        console.log(value1);  
    });
    console.log(value1); 
}
console.log(value1); 

}

Sign up to request clarification or add additional context in comments.

1 Comment

Please do not propose this "solution" without stating all the important drawbacks.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.