从另一个函数调用一个函数

时间:2017-12-14 16:19:33

标签: javascript

我试图从函数count_change调用函数add,但是我得到一个错误,添加不是函数..我做错了什么?

var stat = missing_stat();
function missing_stat(){
    template ="noun verb apples .that is why he is so adj .  also noun verb apples but he is so adj . noun verb oranges ";
    var fields =template.split(' ');
    for (i=0;i<fields.length;i++)
    {
        if(fields[i]=='noun'){
            fields[i]= noun_change();
        }
        var add = (function () {
            var counter = 0;
            return function () {return counter += 1;}
        })();

        function count_change(){
            var count_noun= add();
        }
    }
}

2 个答案:

答案 0 :(得分:0)

您在DELIMITER // CREATE PROCEDURE spMyProc ( IN Val1 VARCHAR(10), IN Val2 VARCHAR(10) ) BEGIN SELECT * FROM table WHERE col1 = Val1 AND col2 = Val2; END // DELIMITER ; 中使用额外的括号只需删除它们,请在下次使用正确的制表时难以定义编码风格中的错误

var add = (function() {...

答案 1 :(得分:0)

首先,我假设您if内部要拨打count_change,而不是noun_change

当您致电count_change时,add尚未定位。搜索&#34;关闭&#34;。

您可以通过在调用add之前移动count_change的定义来避免这种情况,就像这样

var stat = missing_stat();
function missing_stat(){
    template ="noun verb apples .that is why he is so adj .  also noun verb apples but he is so adj . noun verb oranges ";
    var fields =template.split(' ');
    for (i=0;i<fields.length;i++)
    {
        var add = (function () {
            var counter = 0;
            return function () {return counter += 1;}
        })();

        if(fields[i]=='noun'){
            fields[i]= noun_change();
        }


        function count_change(){
            var count_noun= add();
        }
    }
}

此外,每次拨打counter时,add都会重置,因此我不确定您在此处尝试做什么。