尝试在类中创建属性,然后设置其值

时间:2019-03-07 17:51:01

标签: javascript

我以为我做对了,但错误提示:

测试失败:

  

应该是具有名称,电子邮件和密码属性的类。   预期未定义为“丹”。

我看过MDN和w3schools,但显然我只是不了解它。我知道我已经接近了。

function ClassOne(name, pw, mail){
   /*Exercise One: In this exercise, you will be creating your own class!
   You are currently in the class, you are given three strings, name, pw, and mail.
   You need to create three properties on this class.
   Those properties are: 'username', 'password', and 'email'
   Set the value of username to name,
   Set the value of password to pw,
   Set the value of email to mail */
    }
  function User(username, password, email) {
      this.username = 'name';                 //<-------My Code
      this.password = 'pw';                   //<-------My Code
      this.email = 'mail';                    //<-------My Code
  }
 const dan = new User ('Dan', '123xyz', 'dan@aol.com');          //<-------My Code

2 个答案:

答案 0 :(得分:2)

您需要将属性设置为与传递给构造函数的参数相等。现在,您正在将值传递给构造函数,但没有使用它们。进入构造函数后,就可以将属性值设置为文字字符串。尝试这样:

function ClassOne(username, password, email) {
  this.username = username;
  this.password = password;
  this.email = email;
}
const dan = new ClassOne('Dan', '123xyz', 'dan@aol.com');

console.log(dan);

答案 1 :(得分:0)

  this.username = name;
  this.password = pw;
  this.email = mail;

这就是解决方案所需的全部内容。