Javascript 从函数构造函数访问方法
retirementAge方法如何在同一个人函数构造函数中访问calculateAge方法的值Javascript 从函数构造函数访问方法,javascript,Javascript,retirementAge方法如何在同一个人函数构造函数中访问calculateAge方法的值 var Person=function(姓名、职务、出生年份){ this.name=名称; this.yearOfBirth=出生年份; this.calculateAge=函数(){ console.log(2018年-今年出生) }; this.retirementAge=函数(el){ console.log(66-this.calculateAge) } } var john=新人(“约翰
var Person=function(姓名、职务、出生年份){
this.name=名称;
this.yearOfBirth=出生年份;
this.calculateAge=函数(){
console.log(2018年-今年出生)
};
this.retirementAge=函数(el){
console.log(66-this.calculateAge)
}
}
var john=新人(“约翰”,“老师”,1998年);
退休年龄(john.calculateAge())
如果要通过调用来获取值,则需要从this.calculateAge
返回一个值。执行此操作时,只需调用函数this.calculateAge()
,它就可以正常工作:
let Person=函数(姓名、职务、出生年份){
this.name=名称;
this.yearOfBirth=出生年份;
this.calculateAge=函数(){
2018年回归-今年出生
};
this.retirementAge=函数(el){
返回66-this.calculateAge()
}
}
var john=新人(“约翰”,“老师”,1998年);
log(“年龄:,john.calculateAge())
console.log(“退休年限:”,john.retirementAge())
如果要通过调用此函数获取值,则需要从this.calculateAge中返回一个值。执行此操作时,只需调用函数this.calculateAge()
,它就可以正常工作:
let Person=函数(姓名、职务、出生年份){
this.name=名称;
this.yearOfBirth=出生年份;
this.calculateAge=函数(){
2018年回归-今年出生
};
this.retirementAge=函数(el){
返回66-this.calculateAge()
}
}
var john=新人(“约翰”,“老师”,1998年);
log(“年龄:,john.calculateAge())
console.log(“退休年限:”,john.retirementAge())
它可以访问它,您只是忘记了括号并返回了值
var Person = function(name, job, yearOfBirth) {
this.name = name
this.yearOfBirth = yearOfBirth
this.calculateAge = function() {
return 2018 - this.yearOfBirth
};
this.retirementAge = function(el) {
return 66 - this.calculateAge()
}
}
var john = new Person('John', 'teacher', 1998);
console.log(john.retirementAge(john.calculateAge()))
它可以访问它,您只是忘记了括号并返回了值
var Person = function(name, job, yearOfBirth) {
this.name = name
this.yearOfBirth = yearOfBirth
this.calculateAge = function() {
return 2018 - this.yearOfBirth
};
this.retirementAge = function(el) {
return 66 - this.calculateAge()
}
}
var john = new Person('John', 'teacher', 1998);
console.log(john.retirementAge(john.calculateAge()))
您似乎希望将值从calculateAge
传递到retirementAge
。但是,您的calculateAge
函数不会返回任何内容。在CalculationAge
内部,您尝试访问CalculationAge
,就像它是一个值一样,而实际上它是一个函数……您似乎想要将一个值从CalculationAge
传递到retirementAge
。但是,您的calculateAge
函数不会返回任何内容。在retirementAge
中,您尝试访问calculateAge
,就像它是一个值一样,而实际上它是一个函数。。。