所以測試 property 是否存在, 比較正確的做法是使用 hasOwnProperty, 使用上要特別注意的是 hasOwnProperty 並不會到 prototype chain 去尋找, 只會檢查本身是否有定義 property
> var car = {}; undefined > Boolean(car.engine); false > car.engine = "e1"; "e1" > Boolean(car.engine); true > car.engine = 0; 0 > Boolean(car.engine); false > Boolean(null); false > Boolean(0); false > Boolean(""); false > car.hasOwnProperty("engine"); true > delete car.engine; true > car.hasOwnProperty("engine"); false
0 comments:
Post a Comment