Accessing variables from prototype-defined functions

34 Views Asked by At

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;

}
1

There are 1 best solutions below

1
On

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

}