【问题标题】:how to invoke javascript function and variable from another javascript file如何从另一个 javascript 文件中调用 javascript 函数和变量
【发布时间】:2016-01-07 13:52:23
【问题描述】:

我正在构建一个应用程序来检查用户是否按照提供的说明拍照并匹配面部识别。 我有 8 条指令供用户使用,例如检查他们是否在拍照时微笑,因为我们的面部识别是通过在线 API 'Skybiometry' 实现的。所以我们必须从它的 JSON 值中获取相应的值,如下所示,在另一个 html 文件中,如果结果不成功,我需要调用 VerifyFace() 函数并在另一个 javascript 文件中获取消息和结果变量。我将该函数称为VerifyFace();,然后是result;message;。但是代码不起作用。

function ValuesToCheck(){
    Msgs = Msgs(); 
    RandomMsg = RandomMsg();
    if (RandomMsg == Msgs[0] || RandomMsg == Msgs[1]){
      var check = photos[0].tags[0].attributes[0].glasses[0];
    } else if (RandomMsg == Msgs[2] || RandomMsg == Msgs[3]) {
      var check = photos[0].tags[0].attributes[0].smiling[0];
    } else if (RandomMsg == Msgs[4] || RandomMsg == Msgs[5]) {
      var check = photos[0].tags[0].attributes[0].lips[0];
    } else if (RandomMsg == Msgs[6] || RandomMsg == Msgs[7]) {
      var check = photos[0].tags[0].attributes[0].eyes[0];
    }
    check = "facesDet." + check;
    return check;
}

var result = "Unsuccessful";
var message = "You didn't follow the instruction, please try again.";

function VerifyFace(){
    Msgs = Msgs(); 
    RandomMsg = RandomMsg();
    check = ValuesToCheck();
    var value = check + ".value";
    var confidence = check + ".confidence";
    if (RandomMsg == Msgs[0] && value == true && confidence >= 50){
      console("Users followed the instruction!");
    } else {
      result;
      message;
    } 

    if (RandomMsg == Msgs[1] && value == false && confidence < 50){
      console("Users followed the instruction!");
    } else {
      result;
      message;
    }

    if (RandomMsg == Msgs[2] && value == true && confidence >= 50){
      console("Users followed the instruction!");
    } else {
      result;
      message;
    } 
}

以下是“Skybiometry”的 JSON 输出:

{
"photos" : [
    {
        "url" : "http://tinyurl.com/673cksr",
        "pid" : "F@0c95576847e9cd7123f1e304476b59ae_59ec9bb2ad15f",
        "width" : 375,
        "height" : 409,
        "tags" : [
            {
                "tid" : "TEMP_F@0c95576847e9cd7123f1e304b1dcbe53_59ec9bb2ad15f_56.53_40.83_0_1",
                "recognizable" : true,
                "confirmed" : false,
                "manual" : false,
                "width" : 30.67,
                "height" : 28.12,
                "center" : { "x" : 56.53, "y" : 40.83},
                "eye_left" : { "x" : 66.93, "y" : 33.99},
                "eye_right" : { "x" : 51.73, "y" : 33.99},
                "yaw" : -16,
                "roll" : 0,
                "pitch" : 0,
                "attributes" : {
                    "face" : { "value" : "true", "confidence" : 82 },
                    "gender" : { "value" : "female", "confidence" : 80 },
                    "glasses":{"value" : "true", "confidence" : 100},
                    "dark_glasses":{"value" : "true", "confidence" : 72},
                    "smiling":{"value" : "false", "confidence" : 35}
                }
            }
        ]
    }
],
"status" : "success",
"usage" : {
    "used" : 1,
    "remaining" : 99,
    "limit" : 100,
    "reset_time_text" : "Fri, 21 September 2012 12:57:19 +0000",
    "reset_time" : 1348232239
}
}

【问题讨论】:

  • 表达式result;message; 实际上并没有做任何事情。是什么让您认为它不起作用。
  • 请缩进您的代码。我们无法阅读它(我怀疑你也可以)。
  • 尝试“调试”你的程序。 SO 并不擅长调试程序。你的调试器在这方面要好得多。见ericlippert.com/2014/03/05/how-to-debug-small-programs
  • console('...') 也可能会产生错误
  • 您好,很抱歉没有正确格式化它,我正在缩进代码来发布它,忽略了整洁的格式。刚刚编辑整齐,如果有其他需要编辑的地方请再次指出。

标签: javascript json


【解决方案1】:

我不认为这些台词:

Msgs = Msgs(); 
RandomMsg = RandomMsg();

正在做你想做的事,因为你说它们是变量而不是函数。

如果MsgsRandomMsg 确实是全局变量,那么您可以省略这些行,只在函数中使用MsgsRandomMsg

另一种可能性,例如,如果您想确保您的函数不会更改全局变量,则如下所示:

var Msgs      = window.Msgs      || '';
var RandomMsg = window.RandomMsg || '';

将 globasl 复制为本地(如果不存在,则为空字符串)。

还有这些行:

result;
message;

没有可见的效果。你想把它们放在页面上的某个地方还是类似的地方:

alert('result: ' + result + ", message = " + message);

至少?

【讨论】:

  • 只是声明以防你可能不知道,我的Msgs()函数将返回Msgs的值,所以我使用Msgs()获取变量Msgs,只是变量和函数共享相同的名称.我对这些知识是否正确?
  • 重用名称通常是个坏主意。 var theMsgs = Msgs();(或类似的)不那么麻烦。
  • 感谢您的建议。当我在另一个 javascript 文件中调用这个函数时,是否可以使用函数中使用的变量,如 messageresult 在这种情况下。
猜你喜欢
  • 2019-01-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-11-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多