【问题标题】:Automatically running a function inside an object when that object is called调用对象时自动在对象内部运行函数
【发布时间】:2020-10-10 10:26:52
【问题描述】:

我正在使用 Javascript 构建基于文本的选择 RPG 游戏。我可以在对象中添加函数并运行它们,但是,如果选择了选项(文本节点),我希望函数运行。

目前,我声明一个函数,然后在页面底部调用它。问题是,我希望在选择选项后立即运行。

这是我到目前为止的代码......

function addRadiatedStatus(){
    character.radiated = true;
}

const textNodes = [
    {
        id: 1,
        text: "Do you want to help this person?",
        options: [
           {
            text: "Yes",
            nextText: 2
           },
           {
            text: "No",
            nextText: 2
           },
        ],
        faction: "Greaser"
    },
    {
        id: 2,
        text: "It was a trap, do you run or kill them?",
        options: [
           {
            text: "Run",
            nextText: 3,
            damage: 0,
           },
           {
            text: "Kill",
            nextText: 5,
            damage: 8,
            moneyDrop: 20,
            radiationDamage: 2,
            function: function update(){
            if(character.greaser == true && textNodes[1].faction == 
            "greaser"){
                console.log("greaser check worked");
            }
                console.log("upadte function worked")
            character.health -= this.damage;
            character.cash += this.moneyDrop;
            character.radiationLevel += this.radiationDamage;
            character.maxHP -= this.radiationDamage;
            if(this.radiationDamage >= 1 ){
                addRadiatedStatus();
            }
            console.log("character HP" + " " +  character.health);
            console.log("character maxHP" + " " +  character.maxHP);
            console.log("moneyDrop " + " " +  this.moneyDrop);
            console.log("character cash" + " " +  character.cash);
            console.log("radiation level" + " " +  character.radiationLevel);
            console.log("character Raditated?" + " " +  character.radiated);

            }
           }
        ],
        faction: "Greaser",
    }
]

textNodes[1].options[1].function();

我尝试了一些不同的方法,包括添加另一个密钥 run: update()run: this.update()run: this.function 等。

我也有点困惑,因为我认为 ES6 允许我删除 function 关键字,但这似乎是不允许的(我可能只是做错了,或者基于如何我设置了我的对象)。

我不确定如何从调用 textNodes[1].options[1].function 到动态调用嵌套在特定选项选项中的任何函数。

提前感谢您的帮助。

【问题讨论】:

    标签: javascript function oop


    【解决方案1】:

    您可以将此部分更改为:

    update: (function(){
            console.log("upadte function worked")
            character.health -= this.damage;
            character.cash += this.moneyDrop;
            character.radiationLevel += this.radiationDamage;
            character.maxHP -= this.radiationDamage;
            if(this.radiationDamage >= 1 ){
                addRadiatedStatus();
            }
            console.log("character HP" + " " +  character.health);
            console.log("character maxHP" + " " +  character.maxHP);
            console.log("moneyDrop " + " " +  this.moneyDrop);
            console.log("character cash" + " " +  character.cash);
            console.log("radiation level" + " " +  character.radiationLevel);
            console.log("character Raditated?" + " " +  character.radiated);
        });
    

    function 关键字已更改为update,然后该函数已更改为匿名函数...您应该避免将 js 关键字用于用户定义的关键字。 然后匿名函数在你调用它之前不会执行:

    textNodes[1].options[1].update()
    

    下面是我所说的一个例子: 这表明您必须实际访问该函数和任何其他键才能访问这些值。

    var text = [
        {
              data1: "here",
        	  data2: (function(){
    	       	 return "You have to explicitly call me!"
    	  })
        }
    ]
    console.log("Without calling any data:   " + text[0])
    console.log("When you call data2:   " + text[0].data2())
    console.log("Let us access data1:   " + text[0].data1)

    【讨论】:

    • 我想你要么误解了,要么我解释不正确。我想避免一起使用textNodes[1]...,因为我不想调用该函数,除非在我的游戏中选择了该选项,并且我不想添加每个调用 textNode 和选项的索引。
    • 我在我的函数中添加了一个编辑。我添加了一个 if/else 来检查问题的 NPC 派系是否与玩家派系相同。这让我遇到了自动调用函数的类似问题,即试图避免手动添加索引。
    • 我花了几分钟才明白你在说什么,但我想我已经明白了 -- 但我会坚持使用我的 update 而不是你的 function关键词。 您希望update 中的函数无需访问即可立即调用。据我所知,这是不可能的,因为该函数是 key 对象内的 key ......该函数在被调用之前无法运行,并且只能通过手动访问它来完成 - - (这就像定义一个普通函数 - 您必须显式调用它才能运行
    • 查看上面编辑过的答案中的 sn-p 来解释我的意思
    猜你喜欢
    • 2010-10-27
    • 1970-01-01
    • 1970-01-01
    • 2021-03-11
    • 1970-01-01
    • 2021-10-19
    • 1970-01-01
    • 2019-12-16
    • 1970-01-01
    相关资源
    最近更新 更多