从原型定义的函数中访问变量

时间:2018-02-20 22:30:31

标签: javascript prototype private-methods public-method private-functions

让我说我有这个功能,我试图获得值1和2的值,这是正确的方法。 1.当退出函数时,如何定义值1和值2 2.如何在不调用“doSomething”函数的情况下获取另一个函数的值

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 个答案:

答案 0 :(得分:0)

您可以尝试从实验函数中声明value1和value2,并为函数中的那些变量赋值。以下是代码:

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

}