-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;

}
Ele
  • 33,468
  • 7
  • 37
  • 75
makusa
  • 1
  • This looks like an [XY Problem](http://xyproblem.info/). 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? – gforce301 Feb 20 '18 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. – Patrick Roberts Feb 20 '18 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? – Bergi Feb 20 '18 at 22:44

1 Answers1

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); 

}

Irshad Shaik
  • 87
  • 1
  • 2
  • 11