js object can be extended four properties and properties (on)

# Js object can be extended four properties and properties (on)

I. Introduction

  • Take the time to once again look at the foundation, after all, in order to build a good building, a solid foundation is necessary, hee hee!

  • Before you start to need to have the prototype, __ proto __, constructor have got to understand, you can take a look at an article I wrote before => channel
  • Before user management system has been almost, by the way added a socket chat, but because of what friends do java background rhythm can not keep up, so the background is the instant messaging node + socket-io writes, because the list of users also involves comparing multi-user privacy concerns, so I set up a page permissions and interfaces permissions, and then following a few open accounts can only see a few pages of it, ha ha. => Channel
  • 1 Experience account: Account number: "123456." Password: "123456"
  • 2 Experience account: Account number: "123123." Password: "123123"


Second, directory

  • Four characteristics of the object properties
  • Scalability object
  • Delete property
  • Detection property
  • Enumerated attribute
  • Property getter and setter

Four characteristics of the three object properties

1. Definitions

1.1 What is a data property?

数据属性就是我们平常看到的对象普通属性。

Characteristic data for the following four attributes:

值(value)
可写性(writable)
可枚举性(enumerable)
可配置性(configurable)

1.2 What is a memory property?

存储器属性是由getter和setter定义的属性

The memory property of the following four characteristics:

读取(get)
写入(set)
可枚举性(enumerable)
可配置性(configurable)

2, method

2.1 acquire property describes a particular attribute of an object

Object.getOwnPropertyDescriptor()
        const log = console.log;
        //返回数据数据的描述
        let aa = {
            x: 1
        };
        log(Object.getOwnPropertyDescriptor(aa, 'x'))
            //返回存储器属性的描述
        let bb = {
            get y() {
                return 2;
            },
        }
        log(Object.getOwnPropertyDescriptor(bb, 'y'))
            //对于一个不存在的属性或者继承属性返回undefined
        log(Object.getOwnPropertyDescriptor({}, 'x'))
        log(Object.getOwnPropertyDescriptor({}, 'toString'))

2.2, so that the new property with certain properties

Object.defineProperty()

Description:

  • defineProperty can not be modified to inherit property
  • defineProperty does not have to contain all four properties for existing properties, the properties are not specified will not be modified, only the specified characteristics to be modified.
  • For the newly created attribute, it defaults to false or undefined.
  • When the configurable set to false, you can not set to true, because non-configurable can not configure their own
  • When the configurable set to true, writable set to false, the value can be changed by configuring the characteristic value
  • When a configurable set to false, writable from true to false, the time can not be set to true from false
        const log = console.log;
        var aa = {
            y: 22
        };
        //添加一个x属性为不可写、不可枚举、可配置
        Object.defineProperty(aa, 'x', {
            value: 1,
            writable: false,
            enumerable: false,
            configurable: true
        })
        log(aa.x);
        aa.x = 2; //尝试修改这个属性会失败,但是不会报错,在严格模式下会报错
        log(aa.x)
        for (let i in aa) {
            //不可枚举数据属性x,但是y可以枚举
            console.log(i)
        }
        //因为这个x属性依然是可以配置的,所以可以通过配置的方式对值进行修改
        Object.defineProperty(aa, 'x', {
            value: 3,
        })
        log(aa.x);
        //将数据属性设置为存储器属性
        Object.defineProperty(aa, 'x', {
            get: function() {
                return 4;
            },
        })
        log(aa.x)

2.3, and modify the characteristics of a plurality of attributes

Object.defineProperties()
        const log = console.log;
        var aa = {};
        Object.defineProperties(aa, {
            x: {value: 1,writable: false,enumerable: true,configurable: false},
            y: {value: 2,writable: false,enumerable: true,configurable: false},
            z: {
                get:function(){
                    return 3
                },
                enumerable: false,
                configurable: false
            }
        })
        for(let i in aa){
            log(i,'---',aa[i])
        }
        log('z','---',aa.z)


Fourth, scalable object

1. Definitions

1.1 What is scalability?

对象可扩展性是指是否可以给该对象添加新的属性

2, method

2.1, will not be subject to extension

Object.preventExtensions()

Description:

  • Once the object is not set to not be expanded can not be converted into expanded
  • Object.preventExtensions affects only the object itself scalability, it still can still add properties to prototype objects.
        const log = console.log;
        var aa = {};
        Object.preventExtensions(aa);
        aa.x = 1;
        log(aa.x)

2.2, detecting whether the object is scalable

Object.isExtensible()
        //在《javascript权威指南》第六版中6.8.3节介绍可扩展性的时候,将isExtensible写为esExtensible了。
        const log = console.log;
        var aa = {};
        var bb = {};
        Object.preventExtensions(aa);
        log(Object.isExtensible(aa))
        log(Object.isExtensible(bb))

2.3, the target closing (Sealed)

Object.seal()

Description:

  • Object.seal may be provided not only scalability objects, attributes may be provided for all objects owned configurability
  • Set the object can not be extended and is not configurable, which means you can not add new attributes to the object, and the existing property can not delete or configure.
  • However, these properties can still write characteristics is configurable
        const log = console.log;
        var aa = {
            y: 2
        };
        Object.seal(aa);
        aa.x = 1;
        log(aa.x);
        log(Object.getOwnPropertyDescriptor(aa, 'y'))
        Object.defineProperty(aa, 'y', {
            writable: false,
        })
        log(Object.getOwnPropertyDescriptor(aa, 'y'))

2.4, detecting whether the object is closed

Object.isSealed()
        const log = console.log;
        var aa = {};
        var bb = {};
        Object.seal(aa);
        log(Object.isSealed(aa));
        log(Object.isSealed(bb))

2.5, the object is frozen (freeze)

Object.freeze()

Description:

  • freeze not only can not be extended to an object and all the property is not arranged, and all objects will be read-only attribute.
  • If the property has a setter accessor method, it will not be affected, you can still assigns them through this method.
        const log = console.log;
        var aa = {
            x: 1
        };
        Object.freeze(aa);
        log(Object.getOwnPropertyDescriptor(aa, 'x'))

2.6 to detect whether an object with frozen

Object.isFreeze()
        const log = console.log;
        var aa = {};
        var bb = {};
        Object.freeze(aa);
        log(Object.isFrozen(aa));
        log(Object.isFrozen(bb));

Guess you like

Origin www.cnblogs.com/Juaoie/p/12004176.html