Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/444.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript OOP中未知未定义_Javascript_Oop_Prototype - Fatal编程技术网

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

我还在学习javascript中的OOP。在浏览一些教程时,我遇到了以下代码

函数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)的结果。我真的不明白……对不起。