简体   繁体   English

如何从 Javascript 中的 function 运行 object 方法?

[英]How to run an object method from a function in Javascript?

I have a specific task: to create an object (instance of a sub-class) within a function, call a method on that object and then return it.我有一个特定的任务:在 function 中创建一个 object(子类的实例),在该 object 上调用一个方法,然后返回它。 These are the classes:这些是类:

// Task 1: Code a Person class
class Person {
    constructor(name = "Tom", age = 20, energy = 100) {
        this.name = name;
        this.age = age;
        this.energy = energy;
    }
    sleep() {
        this.energy += 10;
        console.log("Energy has increased, now it's ", this.energy);
    }
    doSomethinFun() {
        this.energy -= 10
        console.log("Energy has decreased, now it's ", this.energy);
    }
}

// Task 2: Code a Worker class
class Worker extends Person {
    constructor(name, age, energy, xp = 0, hourlyWage = 10) {
        super(name, age, energy);
        this.xp = xp;
        this.hourlyWage = hourlyWage;
    }
    goToWork() {
        this.xp += 10
        console.log("XP is nov increased, now it's ", this.xp);
    }
}

I've tested all methods on a custom object instantiated from Worker - they all work.我已经在从 Worker 实例化的自定义 object 上测试了所有方法 - 它们都有效。 Now the problem(s):现在问题:

  1. The task: Inside the intern function instantiate the Worker class to code a new intern object.任务:在实习生 function 内部实例化Worker class 以编写新实习生 object。 The intern should have the following characteristics:实习生应具备以下特点:

    • name: Bob姓名:鲍勃
    • age: 21年龄:21
    • energy: 110能量:110
    • xp: 0经验值:0
    • hourlyWage: 10时薪:10

    As I come to find out they cannot have the same name, so I figured to name the object intern1 because function name was predefined by the task.当我发现它们不能具有相同的名称时,所以我想命名 object intern1 因为 function 名称是由任务预定义的。

     function intern() { var intern1 = new Worker("Bob", 21, 110, 0, 10); console.log(intern1); }
  2. The task: Run the goToWork() method on the intern object.任务:在实习生 object 上运行goToWork()方法。 Then return the intern object.然后return实习生 object。

     intern1.goToWork(); return intern();

    I guessed the return is on the function which will automatically invoke the object, and so it works.我猜想返回在 function 上,它会自动调用 object,所以它可以工作。 However intern1.goToWork() doesn't.但是 intern1.goToWork() 没有。 The console throws ReferenceError: intern1 is not defined... as I found out the problem is of scope... but I cannot find the solution.控制台抛出 ReferenceError: intern1 is not defined... 因为我发现问题出在 scope... 但我找不到解决方案。 How to execute goToWork() method on intern1?如何在 intern1 上执行 goToWork() 方法?

the function intern() doesnt return anything and yet you returned nothing. function intern() 没有返回任何东西,但你什么也没返回。 If i understood correnctly, you can do something like that:如果我理解正确,你可以这样做:

function intern() {
    return new Worker("Bob", 21, 110, 0, 10);
}
let intern1 = intern();
intern1.goToWork();

This works 这有效

function intern() {
  var intern1 = new Worker("Bob", 21, 110, 0, 10);
  intern1.goToWork();
  return intern1;
  }

  const i = intern();
  console.log(i);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM