在javascript中创建带有方法的类

时间:2019-03-06 17:53:55

标签: javascript class

我已经为此工作了好几天,到目前为止没有运气。每次我运行这段代码时,都会告诉我,根据我执行函数的方式,它应该为true(或false)。

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.

  this.username = name,
  this.password = pw,
  this.email = mail
  this.checkPassword = function checkPassword() {
    return this.password 
  }

  }

4 个答案:

答案 0 :(得分:2)

您缺少tested_pw函数的checkPassword参数

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.

  this.username = name,
  this.password = pw,
  this.email = mail
  this.checkPassword = function checkPassword(tested_pw) {
    return this.password === tested_pw
  }

  }

答案 1 :(得分:0)

这是我的解决方案。我是javascript的初学者,但这对我有用。

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.

  this.username = name;
  this.password = pw;
  this.email = mail;
  this.checkPassword = function(pw){
    return this.password === pw;
  };
}

答案 2 :(得分:0)

放慢脚步,看着它;拼凑起来:

this.username = name;
this.password = pw;
this.email = mail;
this.checkPassword = function(str) {    
  if (this.password === str) {    
    return true;    
  }
  else (this.checkPassword !== this.password); {
    return false;
  }
};

答案 3 :(得分:0)

您只需要仔细阅读说明即可。

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.

  this.username = name;
  this.password = pw;
  this.email = mail;
  this.checkPassword = function(testPass){
    return this.password === testPass;
  };
}