gpt4 book ai didi

javascript - 使用构造函数创建一个新对象并在 javascript 中调用函数

转载 作者:行者123 更新时间:2023-11-28 08:12:26 24 4
gpt4 key购买 nike

我在使用这段代码时遇到了一些问题:

window.onload = function() {
// Make a new method isLeapYear and accept the date argument.
Date.prototype.isLeapYear = function(date) {
if(date.getFullYear() % 4 != 0) {
return false;
} else if(date.getFullYear() % 100 != 0) {
return true;
} else if(date.getFullYear() % 400 != 0) {
return false;
} else {
return true;
}
}


// Make a new method daysInMonth and accept the date argument.
Date.prototype.daysInMonth = function(date) {
if(date.getMonth() === 0 || date.getMonth() === 2 || date.getMonth() === 4 || date.getMonth() === 6 || date.getMonth() === 7 || date.getMonth() === 9 || date.getMonth() === 11) {
return "31";
} else if(date.getMonth() === 3 || date.getMonth() === 5 || date.getMonth() === 8 || date.getMonth() === 10) {
return "30";
} else if(date.getMonth() === 1) {
if(date.isLeapYear(date) === true) {
return "29";
} else {
return "28";
}
}
}

// Make a new method ageInYears and accept the date argument.
Date.prototype.ageInYears = function(date) {
return Math.floor((new Date() - date) / 1000 / 60 / 60 / 24 / 365.2595);
}

function Person(firstname, lastname, mi, birthdate) {
this.firstname = firstname;
this.lastname = lastname;
this.mi = mi;
this.birthdate = birthdate;
this.age = this.birthdate.ageInYears(this.birthdate);
}

Person.prototype.setBirthdate = function(month,day,year) {
currentYear = new Date().getFullYear();
currentMonth = new Date().getMonth();
currentDay = new Date().getDate();

if(month > currentMonth && day > currentDay && year > currentYear || month > currentMonth && year > currentYear) {
return "Month/Day/Year not valid.";
} else {
return (new Date(year, month, day));
}
}

var newGuy = new Person("New", "Guy", "J", Person.setBirthdate(5,7,1989));
}

我收到错误“未捕获类型错误:未定义不是函数 person.js:59”这就是说 undefined 不是新 Person 对象上的函数。怎么会?我被难住了一段时间,有人可以帮我吗?

最佳答案

您的 setBirthdate 方法相当静态,并且不会修改调用它的实例。它不应该放在原型(prototype)上,并且以不同的方式命名。

function Person(firstname, lastname, mi, birthdate) {
this.firstname = firstname;
this.lastname = lastname;
this.mi = mi;
this.setBirthdate(birthdate); // call prototype methods on the instance
}
Person.prototype.setBirthdate = function(birthdate) {
this.birthdate = birthdate;
this.age = this.birthdate.ageInYears(this.birthdate);
};

// place the static validation function on the constructor itself:
Person.makeBirthdate = function(month,day,year) {
var currentYear = new Date().getFullYear(),
currentMonth = new Date().getMonth(),
currentDay = new Date().getDate();

if (month > currentMonth && day > currentDay && year > currentYear || month > currentMonth && year > currentYear) {
throw "Month/Day/Year not valid.";
} else {
return new Date(year, month, day);
}
}
// where you can call it before having instantiated the `newGuy`
var newGuy = new Person("New", "Guy", "J", Person.makeBirthdate(5,7,1989));

关于javascript - 使用构造函数创建一个新对象并在 javascript 中调用函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24030177/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com