【问题标题】:Have trouble on defining object in javascript file在 javascript 文件中定义对象时遇到问题
【发布时间】:2020-05-21 17:21:05
【问题描述】:

我在 asp.net core mvc 上做一个项目,我在 javascript 文件中有一个问题。我有一个名为“parsedJson”的对象,其中包含几个参数(如“状态”)。当我运行该项目时,我遇到了这个问题。我在这样的函数中描述了 parsedJson

function checkStatus(myID) {
    $.ajax(
        {
            url: '/MemoryGame/checkStatus',
            type: 'POST',
            data: { 'ID': myID },
            dataType: 'json',
            success: function (response) {
                var parsedJson = response;
                if (parsedJson.ifQuit == "True") {
                    OpponentQuit(parsedJson.myID);
                }
                else {
                    else if (parsedJson.status == "Won") {
                        FunctionName(parsedJson);
                    }
                    else if (parsedJson.status == "Lost") {
                        FunctionName(parsedJson);
                    }
                    else if (parsedJson.status == "Tie") {
                        FunctionName(parsedJson);
                    }
                }
            },
            error: function (errorThrown) {
                console.log(errorThrown);
                ConnectionChanged();
            }
        });
}

function FunctionName(parsedJson)
{  
   ...
    if (parsedJson.status == "Won") {
        YouWon(parsedJson.myID);
    }
    else if (parsedJson.status == "Lost") {
        YouLost(parsedJson.myID);
    }
    else if (parsedJson.status == "Tie") {
        GameTied(parsedJson.myID);
    }
}

顺便说一句,parsedJson 文件不为空,每次运行时状态都有自己的值。 谁能建议我在函数中定义对象的正确方法?

【问题讨论】:

  • 请将您的代码发布为文本,而不是图像,并包含定义parsedJson 的代码部分。
  • 我在帖子中添加了它们
  • 我是根据现有代码测试的,没有报错。你的js文件的完整代码是什么?
  • @XueliChen 我也看不懂,因为这段代码适用于这个项目的另一个文件

标签: javascript asp.net-core model-view-controller


【解决方案1】:

我认为 Javascript 对象通常定义为:

let newObject = {
    property1: 'a string',
    property2: 3,
    property3: true
}

然后可以将这些属性引用为 newObject.property1、newObject.property2 等。

关于 Javascript 对象的 W3Schools 页面是 here

【讨论】:

  • 我想我问错了。我需要定义一个对象作为函数参数
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多