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:
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
6.2.3 Property Access Errors的更多相关文章
- 消除警告"property access result unused - getters should not be used for side effects"
我写了如下一段代码: - (void)btnClicked:(UIButton *)button { switch (button.tag) { : self.initShare; break; de ...
- 使用asp.net MVC的 HtmlHelper 时遇到的小问题,报错:Templates can be used only with field access, property access, single-dimension array index, or single-parameter custom indexer expressions.
异常信息:Templates can be used only with field access, property access, single-dimension array index, or ...
- [Javascript] Intercept property access with Javascript Proxy
A Javascript Proxy object is a very interesting es6 feature, that allows you to determine behaviors ...
- [Javascript] Safer property access with Lodash's 'get' method
Property access in Javascript can be problematic - especially when dealing with nested Objects and A ...
- php中直接执行mysqli_init()也是报Property access is not allowed yet的错误。
xdebug.auto_trace = On 和 xdebug.profiler_enable = On注释掉就OK了,不知道这两个配置项是干嘛的
- python 随笔(property & __class__)
1. 属性装饰器: property @property def errors(self): """ Returns a list of form.errors for ...
- JavaPersistenceWithHibernate第二版笔记-第五章-Mapping value types-001Mapping basic properties(@Basic、@Access、access="noop"、@Formula、@ColumnTransformer、@Generated、 @ColumnDefaul、@Temporal、@Enumerated)
一.简介 在JPA中,默认所有属性都会persist,属性要属于以下3种情况,Hibernate在启动时会报错 1.java基本类型或包装类 2.有注解 @Embedded 3.有实现java.io. ...
- Spring 3.1 M1: Unified Property Management(转)
In the first two posts of this series, I described the bean definition profiles feature, and how it ...
- Access与SQL Server 语法差异
序号 简述 Access语法 SqlServer语法 Oracle语法 解决方案 01 系统时间 Now(),Date() GETDATE() SYSDATE GetSysTimeStr 02 连接字 ...
随机推荐
- Java md5加密 控制台传入与web传入参数 结果不匹配 || 相同字符串加密结果不同,如何保证JAVA MD5加密结果在不同的环境下都相同
开发中遇到md5加密不一致问题,排除了上下文编码,加密内容问题. 爬了各类资料,最终找到了原因. /** 对字符串进行MD5加密 */ private static String encodeByMD ...
- [AX]AX2012 R2 EP员工自助服务中的产品不能显示图片的问题
在员工自助服务EP站点中员工可以通过Order products自助提交采购申请,在正确设置员工采购目录后会罗列出允许员工购买的产品,每个产品都可带有图片,我们可以通过Product image来为产 ...
- 【视频】ffmpeg mov mp4 m3u8 ts
1.https://ffmpeg.zeranoe.com/builds/ 2.https://blog.csdn.net/psh18513234633/article/details/79312607 ...
- Splash args 属性
args属性可以获取加载时配置的参数,一般我们只传入URL,如下,args.url 就相当于加载时配置的URL参数,我们把它赋值给 url 变量然后返回:
- ORACLE常用函数汇总【转】
PL/SQL单行函数和组函数详解 函数是一种有零个或多个参数并且有一个返回值的程序.在SQL中Oracle内建了一系列函数,这些函数都可被称为SQL或PL/SQL语句, 函数主要分为两大类: 单行函数 ...
- FileReader类和FileWriter类的基本用法示例
package com.example.io; import java.io.File; import java.io.FileReader; import java.io.FileWriter; i ...
- Python学习(26):Python函数式编程
转自 http://www.cnblogs.com/BeginMan/p/3509985.html 前言 <core python programming 2>说: Python不大可能 ...
- ARC下带CF前缀的类型与OC类型转换
在对钥匙串操作时这个函数 OSStatus SecItemCopyMatching(CFDictionaryRef query, CFTypeRef * __nullable CF_RETURNS_R ...
- vuex报错: [vuex] Expects string as the type, but found undefined.
报错如图 检查了好久,发现 import * as types from '../mutation-types'const actions = { add({commit}){ commit(type ...
- TFS Build做Web应用持续集成发布的一个技巧
由于面向接口编程的关系,许多实现往往是动态注入运行,在一个项目中直接引用实现dll编译是不合理的.通常我们会在Post Build Event中添加一些xcopy命令将运行时才需要的dll复制到输出目 ...