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年前关闭。
            
                    
我有以下代码:

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();

10-04 22:55
查看更多