在javascript中,以下代码之间有什么区别? [重复]

将函数作为函数构造函数的方法包括在内,与通过object.prototype.functionName(){}单独进行处理之间有什么区别?

两者之间的区别

var Person = function(birthYear, job, gender){
    this.birthYear = birthYear;
    this.job = job;
    this.gender = gender;
    this.calcAge = function(){
        console.log(2020 - this.birthYear);
    }
}

和这个

var Person = function(birthYear, job, gender){
    this.birthYear = birthYear;
    this.job = job;
    this.gender = gender;
}

Person.prototype.calcAge = function(){
        console.log(2020 - this.birthYear);
    }
评论