Ray's playground

 

Objects(Chapter 3 of JavaScript: The Good Parts)

  The undefined value is produced if an attempt is made to retrieve a nonexistent member:

  Attempting to retrieve values from undefined will throw a TypeError exception. This can be guarded against with the && operator:

  flight.equipment                              // undefined
  flight.equipment.model                        // throw "TypeError"
  flight.equipment && flight.equipment.model    // undefined

  There are two approaches to dealing with these undesired properties. The first is to have your program look for and reject function values. Generally, when you are reflecting, you are interested in data, and so you should be aware that some values could be functions.

  The other approach is to use the hasOwnProperty method, which returns true if the object has a particular property. The hasOwnProperty method does not look at the prototype chain.

posted on 2010-07-08 12:47  Ray Z  阅读(210)  评论(0编辑  收藏  举报

导航