JavaScript: The Definitive Guide, Sixth Editio javaScript权威指南(第6版) pdf 文字版-文字版, javascript电子书, 和javascript 有关的电子书:

6.2.3 Property Access Errors

6.2.3 Property Access Errors


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:

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 nulland 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:

// A verbose and explicit technique

var len = undefined;

if (book) {

if (book.subtitle) len = book.subtitle.length;

}

// A concise and idiomatic alternative to get subtitle length or undefined var len = book && book.subtitle && book.subtitle.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 properties 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. In strict mode, any failed attempt to set a property throws a TypeError exception.

6.2 Querying and Setting Properties | 123

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 properties. (See the defineProperty() method, however, for an exception that allows configurable read-only properties to be set.)

• ohas an inherited property pthat is read-only: it is not possible to hide an inherited read-only property with an own property of the same name.

欢迎转载,转载请注明来自一手册:http://yishouce.com/book/1/27823.html
友情链接It题库(ittiku.com)| 版权归yishouce.com所有| 友链等可联系 admin#yishouce.com|粤ICP备16001685号-1