Javascript OOP中未知未定义
我还在学习javascript中的OOP。在浏览一些教程时,我遇到了以下代码Javascript OOP中未知未定义,javascript,oop,prototype,Javascript,Oop,Prototype,我还在学习javascript中的OOP。在浏览一些教程时,我遇到了以下代码 函数Employee(){} Employee.prototype.firstName=“Abhijit”; Employee.prototype.lastName=“Patel”; Employee.prototype.startDate=新日期(); Employee.prototype.signedNDA=true; Employee.prototype.fullName=函数(){ console.log(t
函数Employee(){}
Employee.prototype.firstName=“Abhijit”;
Employee.prototype.lastName=“Patel”;
Employee.prototype.startDate=新日期();
Employee.prototype.signedNDA=true;
Employee.prototype.fullName=函数(){
console.log(this.firstName+“”+this.lastName);
};
var abhijit=新员工()//
console.log(abhijit.fullName());//Abhijit Patel&未定义
console.log(abhijit.signedNDA);//true
执行console.log(abhijit.fullName())
分为以下几部分:
function Employee(first, last, signedNDA) {
this.firstName = first;
this.lastName = last;
this.startDate = new Date();
this.signedNDA = signedNDA;
}
1) 执行console.log(abhijit.firstName+“”+abhijit.lastName)
,它来自内部abhijit.fullName()
调用,该调用打印字符串,但不返回任何内容(比如未定义的)
2) 执行外部console.log(未定义)
其中undefined
是(1)的结果
要避免这种行为,您需要稍微更改代码:
Employee.prototype.fullName = function () {
return this.firstName + " " + this.lastName;
};
console.log(abhijit.fullName());
或
此外,我建议不要在原型上保存特定数据,而是在实例上保存:
function Employee(first, last) {
this.firstName = first;
this.lastName = last;
}
Employee.prototype.fullName = function () {
return this.firstName + " " + this.lastName;
};
var abhijit = new Employee('Abhijit', 'Patel');
console.log(abhijit.fullName());
执行console.log(abhijit.fullName())
分为以下几部分:
function Employee(first, last, signedNDA) {
this.firstName = first;
this.lastName = last;
this.startDate = new Date();
this.signedNDA = signedNDA;
}
1) 执行console.log(abhijit.firstName+“”+abhijit.lastName)
,它来自内部abhijit.fullName()
调用,该调用打印字符串,但不返回任何内容(比如未定义的)
2) 执行外部console.log(未定义)
其中undefined
是(1)的结果
要避免这种行为,您需要稍微更改代码:
Employee.prototype.fullName = function () {
return this.firstName + " " + this.lastName;
};
console.log(abhijit.fullName());
或
此外,我建议不要在原型上保存特定数据,而是在实例上保存:
function Employee(first, last) {
this.firstName = first;
this.lastName = last;
}
Employee.prototype.fullName = function () {
return this.firstName + " " + this.lastName;
};
var abhijit = new Employee('Abhijit', 'Patel');
console.log(abhijit.fullName());
如前所述,您的问题是fullName()方法不返回任何内容,因此是未定义的结果
并不是您所问的,但是您的示例还混淆了实例和类类型数据。考虑改变以下内容:
function Employee(first, last, signedNDA) {
this.firstName = first;
this.lastName = last;
this.startDate = new Date();
this.signedNDA = signedNDA;
}
如前所述,您的问题是fullName()方法不返回任何内容,因此是未定义的结果
并不是您所问的,但是您的示例还混淆了实例和类类型数据。考虑改变以下内容:
function Employee(first, last, signedNDA) {
this.firstName = first;
this.lastName = last;
this.startDate = new Date();
this.signedNDA = signedNDA;
}
因为fullName()
不返回任何内容console.log(abhijit.fullName())
相当于console.log(未定义)
abhijit.fullName()
不显示未定义的。您正在console.log(abhijit.fullName())
中记录abhijit.fullName()
的返回值,它是未定义的
@Phil,但为什么它返回abhijit Patel
,而在未定义的
之后它不返回任何内容。没有return
语句。函数本身记录this.firstName+“”+this.lastName
,因为它本身调用console.log
,然后返回undefined
(默认返回值)。“outer”console.log
记录返回值。因为fullName()
不返回任何内容console.log(abhijit.fullName())
相当于console.log(未定义)
abhijit.fullName()
不显示未定义的。您正在console.log(abhijit.fullName())
中记录abhijit.fullName()
的返回值,它是未定义的
@Phil,但为什么它返回abhijit Patel
,而在未定义的
之后它不返回任何内容。没有return
语句。函数本身记录this.firstName+“”+this.lastName
,因为它本身调用console.log
,然后返回undefined
(默认返回值)。“outer”console.log
记录返回值。不完全正确。虽然console.log
确实没有返回任何内容,但OP的代码没有从console.log
返回值,实际上它等于console.log(abhijit.firstName+“”+abhijit.lastName);log((function(){})()
我实际上知道使用return
可以解决这个问题。。我只是很困惑,为什么它也显示未定义的…你是对的,我更新了答案,使之更加清晰accurate@dhilt您能否清楚地解释一下console.log(未定义)
的执行,其中未定义是(1)的结果。我真的不明白……对不起,不完全明白。虽然console.log
确实没有返回任何内容,但OP的代码没有从console.log
返回值,实际上它等于console.log(abhijit.firstName+“”+abhijit.lastName);log((function(){})()
我实际上知道使用return
可以解决这个问题。。我只是很困惑,为什么它也显示未定义的…你是对的,我更新了答案,使之更加清晰accurate@dhilt您能否清楚地解释一下console.log(未定义)
的执行,其中未定义是(1)的结果。我真的不明白……对不起。