卡住!需要使用方法创建一个类,然后创建4个属性

时间:2019-03-07 21:25:48

标签: javascript

出现错误:

测试失败

应包含属性:电子邮件,用户名,密码和checkPassword 预期未定义为“坦率”。

观看了一些视频,在W3Schools和MDN上进行了查找,没有任何点击。输入表示赞赏。

    function ClassTwo(name, pw, mail){
  // Exercise Two: Now that you have created your own class, 
  // you will create a class with a method on it.
  // In this class create 4 properties: username, password, email, and checkPassword.
  // Set the value of username to name,
  // Set the value of password to pw,
  // Set the value of email to mail
  // Set the value of checkPassword to a function. 
  // The checkPassword function takes a string as it's only argument.
  // Using the 'this' keyword check to see if the password on the class is the same as 
  // the string being passed in as the parameter. Return true or false.
}
function User (username, password, email, checkPassword) {

  this.username = name;                           // My Code
  this.password = pw;                             // My Code
  this.email = mail;                              // My Code
  this.checkPassword = checkPass;             // My Code
  this.checkPassword = function(abc123){         // My Code
    return this.checkPassword;                   // My Code 
  }
}

1 个答案:

答案 0 :(得分:0)

在用户功能中分配属性值时,没有使用正确的参数名称。另外,不必将“ checkPassword”作为第四个参数传递。应该是:

function User (username, password, email) {

  this.username = username;
  this.password = password;
  this.email = email;
  this.checkPassword = function(string) {
    return this.password == string;
  }

}

通过这种方式,我们将相同数量的参数传递给函数(例如在“函数ClassTwo(name,pw,mail)”中。)

或者,按照原型方法,您可以创建一个方法:

User.prototype.checkPassword = function(string) {
  return this.password == string; 
}

如果参数字符串与声明的密码相同,则返回true,否则返回false。

此外,更现代的方法是:

class User {
  constructor(username, password, email) {
    this.username = username;
    this.password = password;
    this.email = email;
  }
  checkPassword(string) {
    return this.password == string; 
  }
}