zoukankan      html  css  js  c++  java
  • JavaScript | 数据属性与访问器属性

    属性类型

    数据属性 - 包含一个数据值的位置,可以读取和写入值

    [writable]

    是否能修改属性的值

    true

    [enumerable]

    是否通过for in 循环返回属性(是否可以被枚举)

    true

    [configurable]

    是否能通过delete删除,能否修改属性的特性,能否修改访问器属性

    true

    [value]

    包含这个属性的数据值,读取属性值的时候从这个位置读取。

    undefined

    访问器属性

    [enumerable]

    是否通过for in 循环返回属性(是否可以被枚举)

    true

    [configurable]

    是否能通过delete删除,能否修改属性的特性,能否修改访问器属性

    true

    [get]

    读取属性时调用的函数

    undefined

    [set]

    写入属性时调用的函数

    undefined

    属性操作

    • 定义属性:Object.defineProperty()
    • 查看属性:Object.getOwnPropertyDescriptor()
    "use strict";
    // *****************************************************************
    // 操作数据属性
    var person = {
        name: 'hugh',
        age: 29,
        sayName: function() { console.log(1); }
    }
    // 修改属性默认特性:Object.defineProperty()
    Object.defineProperty(person, "name", {
        writable: true,
        value: 'dong',
        configurable: false,
        enumerable: false
    });
    console.log(person);
    
    // *****************************************************************
    // 操作访问器属性
    var book = {
        _year: 2004, // _作为标记只能通过对象访问的属性
        edition: 0
    };
    Object.defineProperty(book, "year", {
        // 访问器属性year包含setter和getter函数
        get: function() {
            return this._year;
        },
        set: function(newValue) {
            this._year = newValue;
            this.edition = newValue - 2004;
        }
    })
    book.year = 2008;
    console.log(book);
    console.log(book.edition);
    // 旧方法,ie8部分不支持defineProperty()
    // 严格模式不可用
    // book._defineGetter_("year",function(){
    //     return this._year;
    // });
    // book._defineSetter_("year",function(newValue){
    //     this._year = newValue;
    //     this.edition = newValue - 2014;
    // });
    
    // *****************************************************************
    // 定义多个属性
    var book2 = {};
    Object.defineProperties(book2, {
        // 数据属性
        _year: {
            value: 2004,
            writable: false,
            enumerable: false,
            configurable: true
        },
        edition: {
            value: 0,
            writable: false,
            enumerable: false,
            configurable: true
        },
        // 访问器属性
        year: {
            get: function() {
                return this._year;
            },
            set: function(newValue) {
                this._year = newValue;
                this.edition = newValue - 3000;
            }
        }
    });
    console.log(book2);
    
    // *****************************************************************
    // 查看属性的属性
    console.log(Object.getOwnPropertyDescriptor(book2,'_year'));
    console.log(Object.getOwnPropertyDescriptor(book2,'edition'));
    console.log(Object.getOwnPropertyDescriptor(book2,'year'));

  • 相关阅读:
    eclipse 字体设置 Courier New字
    求百分比
    往数据库中插入固定数量的数据
    查看oracle连接数
    c# 简单的一个记事本
    控制台下,查看端口状态命令
    c++ 字符型转整型
    浅析vc6.0的辅助编程工具
    连连看辅助工具
    驱动精灵(Driver Genius Professional Edition 2007) v7.1.622 完美注册版(可升级)
  • 原文地址:https://www.cnblogs.com/hughdong/p/7262539.html
Copyright © 2011-2022 走看看