【问题标题】:how to get my robot to perform work [closed]如何让我的机器人执行工作[关闭]
【发布时间】:2016-02-20 16:17:39
【问题描述】:

我是一个学习js。我正试图让我的机器人工作。这是我想出的:我做错了什么?

var robot ={
  powerOn: false,
  command: "Sweeping the floor",
  duration: 10

  function doWork(command, duration) {
    if (powerOn!= ) {                   
    alert(this.doWork)= "My current task is: " + command + ". Duration: " + duration + " minutes." + BR;  
  }
}   
robot.doWork();

【问题讨论】:

  • 只是好奇,哪个机器人? ^^
  • 请添加更多信息。还要正确格式化代码
  • 我在上面的源代码中创建了一个名为robot的对象
  • 您的代码中有语法错误。您忘记完成if 中的条件语句。除此之外,我们真的不知道你在问什么。你有错误吗?代码没有按照您的预期执行吗?请具体说明,我们实际上并不知道您要完成什么。
  • 你试过阅读javascript教程吗?

标签: javascript methods javascript-objects


【解决方案1】:

我认为您将对象文字语法与新的 class 语法混为一谈。有关对象文字版本,请参阅下面的注释。

var robot = {
  powerOn: false,
  command: "Sweeping the floor",
  duration: 10, // <-- include comma

  // Assign the function like you did with the other object properties
  doWork: function(command, duration) {

    // ---v---reference the robot via `this` (or `robot`) and delete the `!=`
    if (this.powerOn) {   

       // move the `)` to the end and delete the `this.doWork`, `=`, and `+ BR`
      alert("My current task is: " + command + ". Duration: " + duration + " minutes."); 

    } // <--include the closing brace
  }
}   
robot.doWork("vacuum", 10); // pass some data

robot.powerOn = true; // power it on

robot.doWork("vacuum", 10); // try again now that the power is on

【讨论】:

  • 嘿,你在解释她的代码编辑方面做得很好!我赞成你的回答。谢谢!
【解决方案2】:

您的代码存在一些问题。

首先,如果函数doWorkrobot 对象的属性,则需要将其格式化为doWork 是包含函数的属性,如下所示:

doWork: function () {
    if (this.powerOn !== false) {
        alert("My current task is: " + this.command + ". Duration: " + this.duration + " minutes.");
    }
}

我也很困惑,因为您接受了 commandduration 的参数,但是您将它们作为对象的属性而永远不会改变。我将您的代码解释为更喜欢使用robot 的对象属性而不是参数。因此,我删除了这些论点。您可以更新它以接受参数,更改对象的属性,然后触发 alert 以获得相同的结果,但有机会更改任务,如下所示:

doWork: function (command, duration) {
    if (this.powerOn !== false) {

        if (this.command === '') {
            this.command = command;
        }

        if (this.duration === '') {
            this.duration = duration
        }

        alert("My current task is: " + this.command + ". Duration: " + this.duration + " minutes.");
    }
}

您还没有完成对powerOn 属性的评估。据推测,您希望powerOn 在机器人可以尝试做任何工作之前评估为true。我添加了一个启动功能,但您也可以直接更改对象的属性。

我把它放到了fiddle 给你,这样你就可以玩一下了。您还应该查看MDN JavaScript documentation for alert,以便了解如何更好地使用它。

【讨论】:

    【解决方案3】:

    也许你尝试这样做:

    var robot = {
        powerOn: false,
        command: "Sweeping the floor",
        duration: 10
    };
    
    
    function doWork(cmd) {
        if (cmd.powerOn) {
            alert("My current task is: " + cmd.command + ". Duration: " + cmd.duration + " minutes.");
        }
    }
    doWork(robot);
    

    将 powerOn 切换为 true,以获取警报。

    【讨论】:

    • 谢谢。我会试试的。
    • 如何使用this.doWork让机器人做家务?
    • 用“真”代替“假”。
    猜你喜欢
    • 2019-08-26
    • 2021-09-05
    • 1970-01-01
    • 2013-04-15
    • 1970-01-01
    • 2016-07-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多