2
我在尋找理解下面的代碼日誌返回的明顯差異。我期望它們是等效的,不會返回undefined
。但是,一個確實會返回undefined
,而另一個則不會。爲什麼this.prop在構造函數中返回undefined
var floors = [ { number : 4, desc : `The obj from the floors array` } ]
function Unit (number) {
this.number = number
this.floor = Number(String(this.number)[0]) // no issue with a this.property reference here
console.log(this.floor) // evals to 4 here so why not on the next line?
console.log(floors.find(function (floor) { return floor.number === this.floor })) // Why does this return undefined
console.log(floors.find(function (floor) { return floor.number === 4 })) // but this does not?
}
new Unit (425)