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.
 
 

6.2.3 Property Access Errors的更多相关文章

  1. 消除警告"property access result unused - getters should not be used for side effects"

    我写了如下一段代码: - (void)btnClicked:(UIButton *)button { switch (button.tag) { : self.initShare; break; de ...

  2. 使用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 ...

  3. [Javascript] Intercept property access with Javascript Proxy

    A Javascript Proxy object is a very interesting es6 feature, that allows you to determine behaviors ...

  4. [Javascript] Safer property access with Lodash's 'get' method

    Property access in Javascript can be problematic - especially when dealing with nested Objects and A ...

  5. php中直接执行mysqli_init()也是报Property access is not allowed yet的错误。

    xdebug.auto_trace = On 和 xdebug.profiler_enable = On注释掉就OK了,不知道这两个配置项是干嘛的

  6. python 随笔(property & __class__)

    1. 属性装饰器: property @property def errors(self): """ Returns a list of form.errors for ...

  7. 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. ...

  8. 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 ...

  9. Access与SQL Server 语法差异

    序号 简述 Access语法 SqlServer语法 Oracle语法 解决方案 01 系统时间 Now(),Date() GETDATE() SYSDATE GetSysTimeStr 02 连接字 ...

随机推荐

  1. iOS LLDB调试精解

    小笨狼与LLDB: http://jiangliancheng.gitcafe.io/2015/12/13/%E5%B0%8F%E7%AC%A8%E7%8B%BC%E4%B8%8ELLDB%E7%9A ...

  2. tomcat运行模式APR安装

    centos6.2下,Tomcat运行模式apr安装过程,如下: 一.安装apr [root@vmT227-m5 /]# cd /usr/local/ [root@vmT227-m5 local]# ...

  3. Docker 仓库管理

    仓库 ( registry ) 是一个用来存放镜像的地方,当我们执行 docker pull centos 去拉取 centos 镜像时,默认是从 Docker Hub 这个公共仓库来拉取的: 对于企 ...

  4. 解决“Connection to https://dl-ssl.google.com refused”问题

    相信一些人刚开始搞android的安装开发环境的时候会遇到:Failed to fectch URl https://dl-ssl.google.com/android/repository/addo ...

  5. nodeJs学习过程之一个图片上传显示的例子

    目标 1. 在浏览器地址栏输入“http://demos/start”,进入欢迎页面,页面有一个文件上传表单: 2. 选择一张图片并提交表单,文件被上传到"http://demos/uplo ...

  6. tomcat端口被占用的两个解决方法

    tomcat 的 8080 端口经常会被占用,解决办法两个: 1.关闭占用8080端口的进程:8080端口被占用的话执行startup.bat会报错,可在cmd下执行netstat -ano命令查看8 ...

  7. iOS应用内支付(IAP)的那些坑

    本文转载至 http://blog.devtang.com/2013/04/07/tricks-in-iap/ 前言 udacity 中的在线课程 <How to build a startup ...

  8. Java枚举根据key获取value

    package com.utcip.crm.common.constants; import com.utcip.crm.common.base.process.ScheduleStatusEnum; ...

  9. iOS 静态库和动态库(库详解)

    什么是库 ? 库就是程序代码的集合,将N个文件组织起来,是共享程序代码的一种方式.库从本质上来说是一种可执行代码的二进制格式,可以被载入内存中执行. 库的分类 开源库:源代码是公开的,可以看到每个实现 ...

  10. Nginx学习之keepalive

    当然,在nginx中,对于http1.0与http1.1也是支持长连接的.什么是长连接呢?我们知道,http请求是基于TCP协议之上的,那么,当客户端在发起请求前,需要先与服务端建立TCP连接,而每一 ...