【问题标题】:Scope of Javascript in Datapower (GatewayScript)Datapower 中 Javascript 的范围(GatewayScript)
【发布时间】:2016-09-16 08:46:15
【问题描述】:

我是 Datapower 网关脚本(和 Javascript)的新手,以下情况完全让我感到困惑。看:

var inputJson = "default";

//Reading json from input and covert it to string
session.input.readAsJSON( function ( error, json) {
    if ( error ) {
        session.reject( 'Input is not a valid JSON document' );
        return;
    }
    inputJson = JSON.stringify(json);
    console.debug("Inside: ", inputJson);
});

console.debug("Outside ", inputJson);

在 Datapower 控制台中将如下:

“内部:{长json字符串}”

“外部:默认”

这完全打破了我的想法并扭曲了我对变量作用域的了解。是 javascript 的特性,datapower 脚本实现还是什么?

UPD。还有一个脑洞大开的事:

function getFile(options){
    var file="default";
    urlopen.open(options,function(error, response){
        if(error){
            console.error("Unable to find file: "+JSON.stringify(error));
        }else{
            if(response.statusCode==200){
                response.readAsBuffer(function(error, responseData){
                    if(error){
                        console.error("Unable to open file: "+JSON.stringify(error));
                    }else{
                        console.error("Before: ", file);
                        file=responseData.toString('ascii');
                        console.error("After: ", file);
                    }
                });
            }else{
                console.error("Unable to open file: "+response.statusCode);
            }
        }
    });
    return file;
}
console.error("Func result: ", getFile(openSchemaOptions));

控制台结果:

  1. “Func 结果:默认”(原文如此!)

  2. “之前:默认”

  3. “后:--json 字符串--”

如何在函数执行之前打印函数结果?!

【问题讨论】:

    标签: javascript scope ibm-datapower


    【解决方案1】:

    因为session.input.readAsJson(); 将花费更多时间来执行。如果我们在这段代码中对事件的顺序执行进行编号:

    // 1. functions and vars are moved automatically to the top by the js interpreter in your browser. So this is first
    var inputJson = "default";
    
    // 2. a function gets called passing another function as parameter => the callback function of readAsjson
    session.input.readAsJSON(
        // 4. the callback function gets executed
        function ( error, json) {
            if ( error ) {
                session.reject( 'Input is not a valid JSON document' );
                return;
            }
    
            // 5. only executed if no error occured
            inputJson = JSON.stringify(json);
            console.debug("Inside: ", inputJson);
        }
    );
    
    // 3. the console output of inputJson, which was put on 'default'
    console.debug("Outside ", inputJson);
    

    这是 javaScript 的特色,因为只有函数范围。你不能指望readAsJSON()里面的函数参数在“outside”的console.debug之前执行。

    与投掷 2 个回旋镖相比,第一个需要更多时间才能返回。

    类似的行为可以重写:

    function doMyJSON(json){
        // do stuff with json
        console.log(json);
    }
    
    function getMyJSON(error, json){
        if ( error ) { return; }
        doMyJSON(json);
    }
    
    session.input.readAsJSON(getMyJSON);
    

    【讨论】:

    • 感谢您的解释,但我如何通过控制台确保 json-string 已放置在 inputJson 变量中
    • 像你一样在回调中移动它。或者,如果您真的想将其分开,请尝试查找有关“延迟”或“异步”javaScript 的信息。在第 5 步,您可以调用自己的函数,将 json 作为参数传递,然后您将重新开始。
    • 谢谢,很有帮助!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-06-23
    • 2013-10-23
    • 1970-01-01
    • 2012-05-13
    • 2012-05-03
    • 2010-09-24
    • 2013-02-17
    相关资源
    最近更新 更多