Javascript 这样做对吗?我不知道';我不想使用原型,而只是类构造函数上的方法

Javascript 这样做对吗?我不知道';我不想使用原型,而只是类构造函数上的方法,javascript,Javascript,//向此人的原型添加一个名为“isLegalDriver”的方法,如果此人16岁或以上,该方法将返回true function Person(name, age) { this.isLegalDriver = function(){ if(age >= 16){ return true } else { return false }; } } /* Do not modify code below this line */

//向此人的原型添加一个名为“isLegalDriver”的方法,如果此人16岁或以上,该方法将返回true

function Person(name, age) {

  this.isLegalDriver = function(){

    if(age >= 16){
      return true
    } else {
      return false
    };
  }
} 

/* Do not modify code below this line */

const youngPerson = new Person('Jane', 15);
console.log(youngPerson.isLegalDriver(), '<-- should be false');

const olderPerson = new Person('Joan', 16);
console.log(olderPerson.isLegalDriver(), '<-- should be true');
职能人员(姓名、年龄){
this.isLegalDriver=函数(){
如果(年龄>=16岁){
返回真值
}否则{
返回错误
};
}
} 
/*不要修改此行下面的代码*/
const youngPerson=新人('Jane',15岁);
console.log(youngPerson.isLegalDriver(),“没有错。它在对象上创建函数,该函数返回正确的值

我会做出一些改变,但没有错:

  • 我不喜欢依赖自动分号插入,所以我会在函数赋值的末尾添加一个
    (和其他语句,但请看下一个要点)。其他人喜欢ASI,但不喜欢

  • 与其使用
    if
    /
    else
    ,我只返回比较结果

因此:


你可以接受一个IIFE并返回一个带有原型的函数

const Person=function(){
函数p(姓名、年龄){
this.name=名称;
这个。年龄=年龄;
}
p、 prototype.isLegalDriver=函数(){
返回此值。年龄>=16;
}
返回p;
}();
const youngPerson=新人('Jane',15岁);

console.log(youngPerson.isLegalDriver(),“很抱歉,简单地说,这不是你在JS中应该做的工作。你不需要在原型中使用函数。你可以按照以下方法来做

function Person(name, age) {
  this.isLegalDriver = age >= 16 ? true : false;
}

var youngPerson = new Person('Jane', 15);
console.log(youngPerson.isLegalDriver); // returns false

var olderPerson = new Person('Joan', 16);
console.log(olderPerson.isLegalDriver); // return true
但这是愚蠢的,因为年龄不会进步,可怜的家伙可能永远无法获得驾驶执照。为了计算年龄,你需要一个出生日期与当前日期相区别

最后…是的…你不需要这个计算器在实例中,而是在原型中

那你真的喜欢吗

function Person(name, birthDate) {
  this.name      = name;
  this.birthDate = birthDate;
}

Person.prototype.isLegalDriver = function(){
    return (Date.now() - Date.parse(this.birthDate)) / 1000 / 60 / 60 / 24 / 365.242199 >= 16;
}
var john = new Person("John", "7.15.1966"),
    jane = new Person("Jane", "11.24.2003");

john.isLegalDriver() // true
jane.isLegalDriver() // false

这很简洁,我不知道JavaScript与Python中的“理解”类似。我可以在任何地方阅读或了解有关“返回比较结果”的更多信息它使代码变得更短、更清晰,但可能会让像我这样的初学者感到困惑,但我喜欢lol。@Dreadersina-我知道你是如何做到的,但这不像是一种理解,它不涉及列表或序列。当你调用函数时,它会执行
返回年龄>=16;
=
操作符会产生一个布尔值:de>true
如果
=
关系为true,而
关系为false
如果不是,则返回de>true。由于这是您希望从函数返回的内容,因此直接返回它是有意义的。这就像一个
求和
函数执行
返回a+b;
:-)非常容易理解。谢谢你,这不是更长的代码吗?另外,我不想调用Person原型的属性。你把事情复杂化了。你不需要函数来完成这项工作。你需要做的就是
this.isLegalDriver=age>16?true:false;
我不想使用原型,而只想使用类constru上的方法ctor
似乎与
冲突这是正确的方法吗?
。好像你知道使用原型更好,你只是想找人说“是的,这是对的”。你为什么不想使用原型?你错过了写着
/*不要修改这一行下面的代码*/
的部分吗?
function Person(name, birthDate) {
  this.name      = name;
  this.birthDate = birthDate;
}

Person.prototype.isLegalDriver = function(){
    return (Date.now() - Date.parse(this.birthDate)) / 1000 / 60 / 60 / 24 / 365.242199 >= 16;
}
var john = new Person("John", "7.15.1966"),
    jane = new Person("Jane", "11.24.2003");

john.isLegalDriver() // true
jane.isLegalDriver() // false