This question already has answers here:
Why does this JavaScript code print “undefined” on the console?
(1个答案)
Why does the JS console return an extra undefined? [duplicate]
(1个答案)
5年前关闭。
我有以下代码:
控制台输出两个消息。一个是
你可能只想
(1个答案)
Why does the JS console return an extra undefined? [duplicate]
(1个答案)
5年前关闭。
我有以下代码:
function Person() {
this.name = '123';
this.age = 123;
}
Person.prototype.load = function() {
console.log(this.name + " test ");
}
var p1 = new Person();
console.log(p1.load());
控制台输出两个消息。一个是
123 test
,另一个是undefined
。我想知道undefined
是从哪里来的? 最佳答案
load
函数不返回任何内容,即返回undefined
。您在这里记录此undefined
:
console.log(p1.load());
你可能只想
p1.load();