6.2.3 Property Access Errors

JavaScript: The Definitive Guide, Sixth Edition
by David Flanagan
 

Property access expressions do not always return or set a value. This section explains the things that can go wrong when you query or set a property.

It is not an error to query a property that does not exist. If the property x is not found as an own property or an inherited property of o, the property access expression o.x evaluates to undefined. Recall that our book object has a “sub-title” property, but not a “subtitle”  property:

 

 property:book.subtitle;    // => undefined: property doesn't exist

 

It is an error, however, to attempt to query a property of an object that does not exist.The null and undefined values have no properties, and it is an error to query properties of these values. Continuing the above example:

 

 //Raises a TypeError exception. undefined doesn't have a length property 

 var len = book.subtitle.length;

 

Unless you are certain that both book and book.subtitle are (or behave like) objects,you shouldn’t write the expression book.subtitle.length, since it might raise an exception. Here are two ways to guard against this kind of exception:

 

var len = book && book.title && book.title.length

 

To understand why this idiomatic expression works to prevent TypeError exceptions,you might want to review the short-circuiting behavior of the && operator in §4.10.1.

Attempting to set a property on null or undefined also causes a TypeError, of course.Attempts to set properties on other values do not always succeed, either: some prop-erties are read-only and cannot be set, and some objects do not allow the addition of new properties. Curiously, however, these failed attempts to set properties usually fail silently:

 

    //The prototype properties of built-in constructors are read-only.
    Object.prototype = 0; 
    //Assignment fails silently; Object.prototype unchanged

 

This historical quirk of JavaScript is rectified in the strict mode of ECMAScript 5. Instrict mode, any failed attempt to set a property throws a TypeError exception.

The rules that specify when a property assignment succeeds and when it fails are intuitive but difficult to express concisely. An attempt to set a property p of an object o fails in these circumstances:

•o has an own property p that is read-only: it is not possible to set read-only prop-erties. (See the defineProperty() method, however, for an exception that allowsconfigurable read-only properties to be set.) .

•o has an inherited property p that is read-only: it is not possible to hide an inheritedread-only property with an own property of the same name.

•o does not have an own property p; o does not inherit a property p with a setter method, and o’s extensible attribute (see §6.8.3) is false. If p does not already exist on o, and if there is no setter method to call, then p must be added to o. Butif o is not extensible, then no new properties can be defined on it.

 

ECMAScript 5

Object.defineProperty()  create or configure an object property
Synopsis
  Object.defineProperty(o, name, desc)
Arguments
  o
    The object on which a property is to be created or configured.
  name
    The name of the property to be created or configured.
  desc
    A property descriptor object that describes the new property or describes the changes to be made to an existing property.
Returns
  The object o.
 
 
posted @ 2016-09-04 02:18  papering  阅读(269)  评论(0编辑  收藏  举报