【发布时间】:2018-08-08 01:51:46
【问题描述】:
我正在尝试让此代码返回每个员工的姓名。
var company = {
employees: [
{
name: "doug"
},
{
name: "AJ"
}
],
getName: function(employee){
return employee.name
},
getNames: function(){
return this.employees.map(this.getName)
},
delayedGetNames: function(){
setTimeout(this.getNames,500)
}
}
console.log(company.delayedGetNames());
但是,当我运行代码时,我得到“TypeError: Cannot read property 'map' of undefined”
我试过了
setTimeout(this.getNames.bind(this),500)
我只是得到未定义的返回给我。
谁能帮帮我?
【问题讨论】:
-
setTimeout没有返回值。您的console.log将始终返回未定义。你需要使用 Promises。
标签: javascript