从函数返回对象时,我遇到了概念性的 Javascript 问题。有人可以指出我在这里做错了什么吗?
var productsPartsObj = {
layers: ['toe', 'base', 'shaft'],
layer: [
{
name: 'toe',
height: 75,
width: 78,
coords: {
x: 20,
y: 120
}
}
]
}
var coords = {};
coords = (function() {
productsPartsObj.layer.forEach(function(layerObj){
if ( layerObj.name === "toe" ) {
return layerObj.coords;
}
})
})()
console.log(coords); //logs undefined