简体   繁体   English

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

[英]Making a new object using a constructor function and calling a function in javascript

I'm having a little trouble with this code: 我在使用此代码时遇到了一些麻烦:

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));
}

I get the error "Uncaught TypeError: undefined is not a function person.js:59" It is saying that undefined is not a function on the new Person object. 我收到错误“未捕获的TypeError:未定义不是函数person.js:59”,这是说未定义不是新Person对象上的函数。 How come? 怎么会? I've been stumped a while, can anyone help me out? 我被困了一段时间,有人可以帮我吗?

Your setBirthdate method is rather static, and does not modify the instance it is called on. 您的setBirthdate方法相当静态,并且不会修改调用它的实例。 It should not be placed on the prototype, and be named differently. 不应将其放在原型上,而应使用不同的名称。

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));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM