2

所以我有这两个函数,然后是另一个函数调用它们。似乎我在函数调用之间失去了作用域。

var determineInteractionType = function( interaction ){

    var param = interaction.parameterSet.param;

    param.forEach(function( parameter, index, array ){
        if( parameter.name === "INTERACTION-TYPE" ){
            return parameter.value;
        }
    });
    return null;
};

var getInteraction = function( id ){        

    customInteractions.forEach( function(interaction, index, array){            
        if( interaction.id == id ){
            alert( id );
            return interaction;
        }
    });
    return null;
};

这是调用函数的一段代码。错误是即使 getInteraction 返回一个值,它也显示确定Interaction 正在传递一个 null 参数。

var _convertStemFromEAS = function(stem) {
    var reg = new RegExp('@\{PRESENTATION-HTML-INTERACTION\}="(.*?)"');
    var result;
    var count = 1;      

    while ((result = reg.exec(stem)) !== null) {

        var Match = result[0];
        var dropdownGuid = result[1];

//The Error seems to be right here
        var interactionType = determineInteractionType( getInteraction( id ) );


        if( interactionType === "shortTextInteraction" ){           
            var escaped = $('<div/>').text('<select id="' + NewTmpGuid() + '" data-choice-id="' + dropdownGuid + '" style="width:100px;" data-count="' + count + '" class="easSelection"><option>DD' + count + '</option></select>').html();
        }else if( interactionType === "essayTextInteraction" ){

        }

        count += 1;
        stem = stem.replace(Match, escaped);
    }   
    return stem;    
};
4

1 回答 1

1

determineInteraction正在返回 null 因为这就是它被编码返回的内容。

当您return从 forEach 循环中的内部函数返回时,您不会从determineInteraction.

你想要做的是这样的:

var determineInteractionType = function( interaction ){

    var 
    returnValue = null,
    param = interaction.parameterSet.param;


    param.forEach(function( parameter, index, array ){
        if( parameter.name === "INTERACTION-TYPE" ){
            returnValue = parameter.value;
        }
    });

    return returnValue;
};
于 2013-10-04T20:16:43.927 回答