引用对象属性JavaScript

时间:2019-01-31 17:10:02

标签: javascript object reference

我在引用对象时遇到问题。我不明白为什么在功能性动物中我无法获得猫类物体的保护。

const cat ={
  price: 200,
  dollarsSec: 4,
  quantity: 0
};

let image = document.getElementById("cat-shop");
image.addEventListener('click', event => {
  let parseName = event.target.id.toString();
  parseName = parseName.replace("-shop","");
  buy(parseName);
});

function buy (animal) {
  console.log(animal); // This return "cat"
  console.log(animal.price); // This returns undefined. I want cat.price
}
#cat-shop {
  height: 50px;
  width: 50px;
  background: red;
}
<div id="cat-shop"></div>

0 个答案:

没有答案