`
ellen_yang
  • 浏览: 21767 次
  • 性别: Icon_minigender_2
  • 来自: 上海
社区版块
存档分类
最新评论

7.2 对象的属性(Object Properties )

阅读更多

可以通过 . 操作符来存取对象的属性值。 . 操作符左边的值必须为对象,通常它会是包含对象引用的一个变量的名字,也可以是任何返回一个对象的javascript 的表达式。 . 操作符右边的值必须为一个标识符,不能是字符串或一个表达式。

 Object properties work like variables: you can store values in them and read values from them.

(对象的属性类似于变量的工作方式,)

 

 var book = {};

// Set a property in the object.
book.title = "JavaScript: The Definitive Guide"

// Set some more properties. Note the nested objects.
book.chapter1 = new Object();
book.chapter1.title = "Introduction to JavaScript";
book.chapter1.pages = 11;
book.chapter2 = { title: "Lexical Structure", pages: 6 };

// Read some property values from the object.
alert("Outline: " + book.title + "\n\t" +
      "Chapter 1 " + book.chapter1.title + "\n\t" +
      "Chapter 2 " + book.chapter2.title);

 

 

属性的枚举

 

可以用 for/in 来遍历、枚举对象的所有属性

举例:

function DisplayPropertyNames(obj) {
    var names = "";
    for(var name in obj) names += name + "\n";
    alert(names);
}

 

Note:for/in 循环列出的属性没有特定顺序,能列举出所有的用户定义的属性,但不能列举出某些预定义的属性或方法。

 

检查属性是否存在(Checking Property Existence

可以用 in 操作符检测属性是否存在。

The left side of this operator should be the name of the property as a string. The right side should be the object to be tested

 

if ("x" in o) o.x = 1;

 

if you query a property that does not exist, the undefined value is returned

if (o.x !== undefined) o.x = 1;

Note that it is possible for a property to exist but still be undefined. For example, if you write:

 

o.x = undefined;

 

Note also that the !== operator was used earlier instead of the more common != operator. !== and === distinguish between undefined and null

 

// If the doSomething property exists and is not null or undefined,
// then assume it is a function and invoke it!
if (o.doSomething) o.doSomething();

 

  Deleting Properties

You can use the delete operator to delete a property of an object:

delete book.chapter2;

 

Note that deleting a property does not merely set the property to undefined; it actually removes the property from the object. After deletion, for/in will not enumerate the property and the in operator will not detect it.


 

 

 

分享到:
评论

相关推荐

    Visual Prolog 8 Language Reference

    类实现中包含了Prolog实体的声明和定义,这些实体包括域(domains)、常量(constants)、谓词(predicates)、属性(properties)和事实数据库(fact databases)。Visual Prolog的“实际”代码是位于类实现中的...

    面向对象技术与UML课件及源代码-by 南邮-陈杨

    本书为中南大学精品教材立项项目,分为上下两篇共21章,涵盖了面向对象技术中Java开发环境配置、程序设计基础、面向对象原理以及UML的知识。本书使用的开发环境是JDK 1.6+Eclipse 3.3+Rational Rose 2003,逐步引领...

    Jacorb ProgrammingGuide

    CORBA 提供了 ORB(Object Request Broker)的概念,ORB 负责管理对象之间的请求与响应过程,从而实现了对象间的透明通信。 ##### 1.2 项目历史 JacORB 项目始于 1996 年,最初由一群热心的开发者共同维护。随着...

    Hibernate+中文文档

    11.1.3. 关注对象标识(Considering object identity) 11.1.4. 常见问题 11.2. 数据库事务声明 11.2.1. 非托管环境 11.2.2. 使用JTA 11.2.3. 异常处理 11.2.4. 事务超时 11.3. 乐观并发控制(Optimistic ...

    Hibernate_3.2.0_符合Java习惯的关系数据库持久化

    11.1.3. 关注对象标识(Considering object identity) 11.1.4. 常见问题 11.2. 数据库事务声明 11.2.1. 非托管环境 11.2.2. 使用JTA 11.2.3. 异常处理 11.2.4. 事务超时 11.3. 乐观并发控制(Optimistic ...

    HibernateAPI中文版.chm

    11.1.3. 关注对象标识(Considering object identity) 11.1.4. 常见问题 11.2. 数据库事务声明 11.2.1. 非托管环境 11.2.2. 使用JTA 11.2.3. 异常处理 11.2.4. 事务超时 11.3. 乐观并发控制(Optimistic ...

    hibernate3.2中文文档(chm格式)

    11.1.3. 关注对象标识(Considering object identity) 11.1.4. 常见问题 11.2. 数据库事务声明 11.2.1. 非托管环境 11.2.2. 使用JTA 11.2.3. 异常处理 11.2.4. 事务超时 11.3. 乐观并发控制(Optimistic ...

    Free pascal 参考

    ### Free Pascal 参考...类的属性与对象的属性类似。 ```pascal property Field: integer read fField write fField; ``` #### 七、接口 接口定义了一组方法的契约,实现接口的类必须提供这些方法的具体实现。 ###...

    MapX 5.0开发手册

    - **对象属性:** 如数据源路径、字段信息等。 **6.5 Using the Datasets Collection** - **数据集集合:** 存储应用程序中所有数据集的对象。 - **集合操作:** 包括添加、删除、查询等。 **6.6 Using the ...

    hibernate 体系结构与配置 参考文档(html)

    关注对象标识(Considering object identity) 11.1.4. 常见问题 11.2. 数据库事务声明 11.2.1. 非托管环境 11.2.2. 使用JTA 11.2.3. 异常处理 11.2.4. 事务超时 11.3. 乐观并发控制(Optimistic concurrency ...

    Hibernate中文详细学习文档

    11.1.3. 关注对象标识(Considering object identity) 11.1.4. 常见问题 11.2. 数据库事务声明 11.2.1. 非托管环境 11.2.2. 使用JTA 11.2.3. 异常处理 11.2.4. 事务超时 11.3. 乐观并发控制(Optimistic ...

    hibernate_reference中文文档.pdf

    - **5.1.16 属性 (Properties)**:提供关于映射集合属性的信息。 - **5.1.17 子类 (subclass)**:介绍如何使用子类映射来实现继承。 - **5.1.18 连接的子类 (joined-subclass)**:讨论连接子类映射的特点。 - **...

    Hibernate参考文档

    11.1.3. 关注对象标识(Considering object identity) 11.1.4. 常见问题 11.2. 数据库事务声明 11.2.1. 非托管环境 11.2.2. 使用JTA 11.2.3. 异常处理 11.2.4. 事务超时 11.3. 乐观并发控制(Optimistic concurrency ...

    Hibernate 中文 html 帮助文档

    11.1.3. 关注对象标识(Considering object identity) 11.1.4. 常见问题 11.2. 数据库事务声明 11.2.1. 非托管环境 11.2.2. 使用JTA 11.2.3. 异常处理 11.2.4. 事务超时 11.3. 乐观并发控制(Optimistic concurrency ...

    NHibernate参考文档 2.0.0 chm

    7.2. 在集合中出现的依赖对象 (Collections of dependent objects) 7.3. 组件作为IDictionary的索引(Components as IDictionary indices ) 7.4. 组件作为联合标识符(Components as composite identifiers) 7.5. ...

    hibernate 框架详解

    关注对象标识(Considering object identity) 12.1.4. 常见问题 12.2. 数据库事务声明 12.2.1. 非托管环境 12.2.2. 使用JTA 12.2.3. 异常处理 12.3. 乐观并发控制(Optimistic concurrency control) ...

    NHibernate中文帮组文档(2008.11月更新)

    7.2. 在集合中出现的依赖对象 (Collections of dependent objects) 7.3. 组件作为IDictionary的索引(Components as IDictionary indices ) 7.4. 组件作为联合标识符(Components as composite identifiers) 7.5. ...

    hibernate3.04中文文档.chm

    12.1.3. 关注对象标识(Considering object identity) 12.1.4. 常见问题 12.2. 数据库事务声明 12.2.1. 非托管环境 12.2.2. 使用JTA 12.2.3. 异常处理 12.3. 乐观并发控制(Optimistic concurrency control) ...

Global site tag (gtag.js) - Google Analytics