当我运行下面的代码时,输出将是:
function Person(name, age) {
this.name = name;
this.age = age;
this.getName = function() {
const self = this;
function run() {
console.log(self.name); // output "john"
return self.name;
}
run();
};
this.getAge = function() {
return this.age;
};
}
const john = new Person("john", 25);
console.log(john.getName()); // output undefined
console.log(john.getAge()); // output 25
五月
答案 0 :(得分:3)
您的getName
函数不返回值。
this.getName = function() {
const self = this;
function run() {
console.log(self.name); // output "john"
return self.name;
}
return run(); // <- You need to return the value of run()
};