Javascript 类未正确读取构造函数中的参数

Javascript 类未正确读取构造函数中的参数,javascript,class,parameter-passing,Javascript,Class,Parameter Passing,我应该构建一个类用户,它应该包含: 一个目标属性,它等于构造函数函数的第一个参数。 已激活的属性设置为false。 一个名为activate的实例函数,它将activated设置为true。 如果activated属性设置为true,则实例函数use返回实例目标的值。否则未定义 我得到了一个错误:“看起来您的用户(类名)没有正确地从构造函数中读取它的目标。” 我不知道这意味着什么。这是我目前的代码: class User { constructor(target) { this.ac

我应该构建一个
用户,它应该包含: 一个目标
属性
,它等于
构造函数
函数的
第一个参数
。 已激活的
属性设置为
false
。 一个名为
activate
的实例函数,它将
activated
设置为
true
。 如果
activated
属性设置为
true
,则实例函数
use
返回实例目标的值。否则
未定义

我得到了一个错误:“看起来您的用户(类名)没有正确地从构造函数中读取它的目标。”

我不知道这意味着什么。这是我目前的代码:

class User {
  constructor(target) {
    this.activated = false;
    this.activate = () => {
      this.activated = true;
    }
    this.use = () => {
      if (this.activated == true) {
        console.log(target);
      } else {
        console.log(false);
      }
    }
  }
}

const m = new User('Kevin');
m.activate(); // Because this.activated is set to true …
m.use(); // Output is target which is 'Kevin' 
所以我不明白,为什么我错过了考试

因为部分任务要求: “一个目标
属性
,它等于
构造函数
函数的
第一个参数
。” 我想设置

class User {
  constructor(name) {
这样做

this.name = target
…将解决问题

但我得到了:

ReferenceError: target is not defined
可能是因为不懂英语,但我不知道我错过了什么。因为我得到了想要的输出:
当我运行
m.activate()时
m之前。使用()在你的第二次尝试中,我得到了“Kevin”,否则
false

class User {
  constructor(name) {
您的构造函数参数名为“name”,但您正在尝试设置

this.name = target
试着换成

this.name = name;

您需要相应地更新“use”函数,以便它从构造函数开头设置的属性
this.name
中读取名称。

您是否声明了在构造函数中使用的变量?您没有将
target
参数设置为任何属性。在构造函数中设置
this.target=target
,在函数中设置
console.log(this.target)
Thank you@deceze it is m.use();:)@AdilRaza我希望我正确地回答了你的问题:是的,我在构造函数中声明了所有内容–没有进一步的代码。@S.H你解决了问题吗?adiga,非常感谢,你的解决方案帮助了我。