如何更新我的函数表达式没有状态变量?

时间:2017-04-07 18:33:58

标签: javascript jquery

我在jquery中创建游戏,只需点击一下按钮即可创建播放器。这是我用来保存信息的函数表达式:

var getPlayerData = function() {
    return {
        name: $("#name").val(),//get name from input
        office_name: $("#office-name").val(), //get name from input
        score: parseInt($('#score').text(), 10), //players startwith 1k HC
        office_location: "office_loc1", //set Player's office
        office_multiplier: office_loc1, // Set Players office multiplier
        notified: false,
        projects_completed: 0
    };
};

然后我在其他函数中运行此函数来访问播放器的变量,如下所示:

$('#create-player').click(function startProject() {
    getPlayerData();
});

我需要一种方法来使用条件更新office_locationoffice_multiplier变量。办公室乘数正在引用这组变量:

var office_loc1 = .01,
    office_loc2 = .02,
    office_loc3 = .03,
    office_loc4 = .04,
    office_loc5 = .05,

这是我试图更新其中一些函数的条件,这里是if条件的步骤:

  1. 从HTML获得分数,检查它> 0&&
  2. 检查倍数是= office_loc1变量&&
  3. 检查player_notified = false
  4. if (parseInt($('#score').text(), 10) > 0 && getPlayerData().office_multiplier === office_loc1 && getPlayerData().notified === false){ }

    条件中要注意的关键部分是:

    //upgrade players office and notify them
          getPlayerData().office_location = "office_loc2";
          getPlayerData().office_multiplier = office_loc2;
          getPlayerData().notified = true;
    

    但是,这实际上不会更新变量。有没有人对此有任何想法?

    谢谢!

2 个答案:

答案 0 :(得分:3)

每次拨打getPlayerData

时,您都在创建播放器的新对象实例

如果它是一个只读对象,那不会有问题,但是一旦你试图改变它的任何属性,一旦再次调用getPlayerData(),你将失去更新的值。

您应该将playerData保留为实例变量,然后您可以通过代码访问。

var playerData = getPlayerData();

-------------编辑-------------

在定义getPlayerData函数后,立即创建一个var playerData。像这样:

var getPlayerData = function() {
    return {
        name: $("#name").val(),//get name from input
        office_name: $("#office-name").val(), //get name from input
        score: parseInt($('#score').text(), 10), //players startwith 1k HC
        office_location: "office_loc1", //set Player's office
        office_multiplier: office_loc1, // Set Players office multiplier
        notified: false,
        projects_completed: 0
    };
};
var playerData = getPlayerData();

然后,在您呼叫getPlayerData()的每个地方,您都会使用playerData

答案 1 :(得分:0)

第一部分

此代码:

$('#create-player').click(function startProject() {
    getPlayerData();
});

有点可疑。根据Flanagan,函数可以被调用,它可能不会返回任何值,但如果确实如此,那么假设您想要将调用结果存储在某处,只需将函数调用结果赋给变量。

var someVar = getPlayerData();

否则会发生以下情况:

  • 您调用getPlayerData函数
  • 该函数返回一个对象
  • 评估对象
  • 因为缺少赋值运算符,所以会丢弃该对象

好像js会说:"我已经执行了这个功能,我看到了一个物体,但是我不知道我应该怎么做...让我把它留下来它是"

第二部分。我会建议什么..

如果你想创建一个对象,还有另外一种方法。乍一看,我将以下代码视为对象构造函数:

var getPlayerData = function() {
    return {
        name: $("#name").val(),//get name from input
        office_name: $("#office-name").val(), //get name from input
        score: parseInt($('#score').text(), 10), //players startwith 1k HC
        office_location: "office_loc1", //set Player's office
        office_multiplier: office_loc1, // Set Players office multiplier
        notified: false,
        projects_completed: 0
    };
};

除此之外,您可以创建一个具有某些功能的对象,这些功能将更新您要更新的字段。让我演示..

var PLAYER_DATA = function(name, office_name, score, office_location, office_multiplier, notified, projects_completed) {
    var nname = name,
        ooffice_name = office_name,
        sscore = parseInt(score, 10),
        ooffice_location = office_location,
        ooffice_multiplier = office_multiplier,
        nnotified = notified,
        pprojects_completed = projects_completed;

    function setName (name) {
        nname = name;
    }

    function getName() {
        return nname;
    }

    function setOfficeName(officeName) {
        ooffice_name = officeName;
    }

    function getOfficeName() {
        return ooffice_name;
    }

    function setScore(score) {
        sscore = parseInt(score, 10);
    }

    function getScore() {
        return sscore;
    }

    function setOfficeLocation(officeLocation) {
        ooffice_location = officeLocation;
    }

    function getOfficeLocation() {
        return ooffice_location;
    }

    function setOfficeMultiplier(officeMultiplier) {
        ooffice_multiplier = officeMultiplier;
    }

    function getOfficeMultiplier() {
        return ooffice_multiplier;
    }

    function setNotified(notified) {
        nnotified = notified;
    }

    function isNotified() {
        return nnotified;
    }

    function setProjectsCompleted(amount) {
        // is zero by default
        pprojects_completed = (amount > 0) ? amount : 0;
    }

    function getProjectsCompleted() {
        return pprojects_completed;
    }




    return {
        setName: setName,
        getName: getName,
        setOfficeName: setOfficeName,
        getOfficeName: getOfficeName,
        setScore: setScore,
        getScore: getScore,
        setOfficeLocation: setOfficeLocation,
        getOfficeLocation: getOfficeLocation,
        setOfficeMultiplier: setOfficeMultiplier,
        getOfficeMultiplier: getOfficeMultiplier,
        setNotified: setNotified,
        isNotified: isNotified,
        setProjectsCompleted: setProjectsCompleted,
        getProjectsCompleted: getProjectsCompleted
    }
};

之后,您需要创建此对象,将一些初始值作为参数传递给conscructor:

var somePlayerData = new PLAYER_DATA($("#name").val(),
                                     $("#office-name").val(),
                                     // Notice that parseInt will be executed while creation of PLAYER_DATA 
                                     // object so there's no need to parse it again
                                     $('#score').text(),
                                     "office_loc1",
                                     "office_loc1",
                                     false,
                                     0)

之后,您可以通过getter访问对象的值,并通过setter设置一些您想要的值