`

JavaScript Object Properties —— Enumeration, Types of Properties

 
阅读更多

原创转载请注明出处:http://agilestyle.iteye.com/blog/2341899 

 

Enumeration

All object properties are enumerable by default, which means that they will appear in a for-in loop or be retrieved by Object.keys().

var listProperties1 = function (object) {
    var property;

    for (property in object) {
        console.log("Name: " + property);
        console.log("Value: " + object[property]);
    }
}

var listProperties2 = function (object) {
    var properties = Object.keys(object);

    var i, len;
    for (i = 0, len = properties.length; i < len; i++) {
        console.log("Name: " + properties[i]);
        console.log("Value: " + object[properties[i]]);
    }
}

var person1 = {
    name: "Nicholas"
};

listProperties1(person1);
listProperties2(person1);

console.log("name" in person1);                         // true
console.log(person1.propertyIsEnumerable("name"));      // true
var properties = Object.keys(person1);
console.log("length" in properties);                    // true
console.log(properties.propertyIsEnumerable("length")); // false

Note:

Keep in mind that not all properties are enumerable. In fact, most of the native methods on objects have their [[Enumerable]] attribute set to false. You can check whether a property is enumerable by using the propertyIsEnumerable() method, which is present on every object. 

 

Types of Properties

There are two types of properties: data properties and accessor properties.

Data properties are placeholders for values, and you can read from and write to them. When a data property holds a function value, the property is considered a method of the object.

Unlike data properties, accessor properties don’t store values on their own; they use a combination of getters and setters to perform specific actions. You can create both data properties and accessor properties directly using object literal notation.

var person1 = {
    _name: "Kobe",

    get name() {
        console.log("Reading name");
        return this._name;
    },
    set name(value) {
        console.log("Setting name to %s", value);
        this._name = value;
    }
};

console.log(person1.name);  // "Reading name" then outputs "Kobe"
person1.name = "Bryant";
console.log(person1.name);  // "Setting name to Bryant" then outputs "Bryant"

This example defines an accessor property called name. There is a data property called _name that contains the actual value for the property. (The leading underscore is a common convention to indicate that the property is considered to be private, though in reality it is still public). The syntax used to define the getter and setter for name looks a lot like a function but without the function keyword. The special keywords get and set are used before the accessor property name, followed by parentheses and a function body. Getters are expected to return a value, while setters receive the value being assigned to the property as an argument.

Even though this example uses _name to store the property data, you could just as easily store the data in a variable or even in another object. This example simply adds logging to the behavior of the property; there's usually no reason to use accessor properties if you are only storing the data in another property - just use the property itself. Accessor properties are most useful when you want the assignment of a value to trigger some sort of behavior, or when reading a value requires the calculation of the desired return value. 

 

Reference

Leanpub.Principles.of.Object-Oriented.Programming.in.JavaScript.Jun.2014

 

 

 

 

分享到:
评论

相关推荐

    用JAVA轻松操作properties文件

    ### 使用Java轻松操作properties文件 #### 一、概述 在Java开发中,`properties`文件是一种常见的配置文件格式,主要用于存储一系列的键值对。它通常用于保存应用程序的各种配置信息,如数据库连接字符串、服务器...

    java读写properties配置文件

    ### Java读写Properties配置文件详解 #### 一、引言 在Java开发中,`Properties`类被广泛用于处理各种类型的配置文件。这些文件通常包含了应用程序运行时所需的配置信息,如数据库连接信息、系统参数等。`...

    GObject Reference Manual

    Enums and Flags - Enumeration and flags types GBoxed - A mechanism to wrap opaque C structures registered by the type system Generic Values - A polymorphic type that can hold values of any other ...

    java对properties文件的操作.pdf

    Enumeration&lt;String&gt; enumvalue = (Enumeration) properties.propertyNames(); while (enumvalue.hasMoreElements()) { String key = enumvalue.nextElement(); System.out.println(key + " : " + properties....

    Java读取Properties文件的六种方法.txt

    ### Java读取Properties文件的六种方法 在Java开发中,`Properties`类是一个非常实用且常见的工具类,主要用于管理程序中的配置信息。通常情况下,这些配置信息会被存储在一个`.properties`文件中,并通过`...

    Properties和xml读写操作

    根据给定文件的信息,我们可以总结出关于 Java 中 Properties 和 XML 的读写操作的相关知识点。 ### 一、Properties 文件的读写操作 #### 1. 读取 Properties 文件 在 Java 中,`Properties` 类用于处理属性文件...

    Java代码实现对properties文件有序的读写的示例

    public Enumeration&lt;Object&gt; keys() { return Collections.enumeration(keys); } @Override public Object put(Object key, Object value) { keys.add(key); return super.put(key, value); } @Override ...

    Java Properties简介_动力节点Java学院整理

    Java Properties 类是Java标准库中一个非常实用的类,它主要用于处理配置文件,尤其是以`.properties`格式存储的键值对数据。这类文件通常用于存储应用程序的配置信息,如数据库连接字符串、应用设置等,其内容是以...

    java对properties文件的操作[参考].pdf

    Enumeration&lt;String&gt; enumValue = (Enumeration) properties.propertyNames(); while (enumValue.hasMoreElements()) { String key = enumValue.nextElement(); System.out.println(key + " : " + properties....

    Properties 基本知识

    ### Properties基本知识详解 在Java开发中,`java.util.Properties`类被广泛应用于配置文件管理、持久化数据存储以及各种需要键值对映射的场景。根据提供的标题、描述及部分代码内容,本文将深入探讨`Properties`的...

    C程序设计的抽象思维(源码)

    2.1 Enumeration types 2.2 Data and memory 2.3 Pointers 2.4 Arrays 2.5 Pointers and arrays 2.6 Records 2.7 dynamic allocation 3 Liraries and Interfaces PART TWO Recursion and Algorithmic Analysis 4 ...

    Enumeration of spanning trees in planar unclustered networks

    Enumeration of spanning trees in planar unclustered networks

    delphi-object pascal语言梗概

    5. **枚举类型 (Enumeration Types)** - 枚举类型定义了一组有序的常量集合,每个枚举值都有一个关联的整数值,默认情况下,第一个枚举值为 0,后续值依次递增。 - 示例:`type MyEnum = (Key1, Key2, Key3);` 6....

    如何使用RecordStore Enumeration 排序

    经我细心探究,终于把J2ME中的Enumeration排序问题给解决了

Global site tag (gtag.js) - Google Analytics