当前位置:网站首页>Implement const in Es5
Implement const in Es5
2022-06-25 23:56:00 【wen_ Wen Wen】
ES5 Two attribute types in : Data properties and accessor properties ;
Data attribute : Contains the location of a data value , In this location, you can read and write values ; Yes 4 A characteristic that describes its behavior ;
Configuarable: Can we pass delete Delete attribute , And redefine the properties ,
Enumerable: Can we pass for-in Loop returns the property ,
Writable: Whether the value of the property can be modified ;
Value: The data value of this property , The default is undefined
Depending on how you define objects ,Configurable,Enumerable,Writable The default values are also different
The way 1: var person = {name: ' Zhang San '}
be person Object's name Property in the property descriptor object of the Configurable,Enumerable,Writable All values default to true
The way 2:
var person = {}
Object.defineProperty(person,'name',{
value: ' Zhang San '
})
be person Object's name Property in the property descriptor object of the Configurable,Enumerable,Writable All values default to false
// "use strict"
var person = {}
Object.defineProperty(person, 'name', {
value: ' Zhang San '
})
console.log(person.name); // Zhang San
// When strict mode is enabled : "use strict"; Uncaught TypeError: Cannot assign to read only property 'name' of object '#<Object>'
person.name = ' Li Si ';
console.log(person.name); // Zhang San Accessor properties : Does not contain data values , Containing a pair of getter and setter function
Configuarable: Can we pass delete Delete attribute , And redefine the properties ,
Enumerable: Can we pass for-in Loop returns the property ,
Get: Function called when reading properties , The default is undefined
Set: The function called when the property is written , The default is undefined
Accessor properties cannot be defined directly , You have to use Object.defineProperty() To define
// 1. Custom containers act as storage for data
var constObj = {}
function getConst(key, val) {
constObj[key] = val;
Object.defineProperty(constObj, key, {
value: val,
configurable: false, // Whether the configuration information can be modified
writable: false, // Whether the attribute value can be modified
enumerable: true // Enumerable or not
})
}
getConst('a', 10)
constObj.a = 20;
console.log(constObj.a); //10
// 2. Mount to window above
function getConst2(constKey, constVal) {
window[constKey] = constVal;
Object.defineProperty(window, constKey,{
get: function() { // If set set or get, You can't set writable and value Any one of them , Otherwise, the report will be wrong
return constVal;
},
enumerable: true, // Enumerable or not
configurable: false, // Whether the configuration information of the current description can be modified
})
}
getConst2('b', 30);
getConst2('c', 50);
console.log(window.b); //30
console.log(window.c); //50
window.b = 66
console.log(window.b); //30
边栏推荐
- 虚析构和纯虚析构及C ++实现
- mysql5.7版本在配置文件my.ini[mysqld]加上skip-grant-tables后无法启动
- [reprint]rslogix 5000 instance tutorial
- 关于运行scrapy项目时提示 ModuleNotFoundError: No module named 'pymongo‘的解决方案
- Final and static
- 如何设计产品的roadmap?
- Wireshark对IMAP抓包分析
- 如何进行流程创新,以最经济的方式提升产品体验?
- 剑指 Offer 48. 最长不含重复字符的子字符串
- 51 single chip microcomputer, some registers, some knowledge points
猜你喜欢

line-height小用

Use Baidu map API to set an overlay (infowindow) in the map to customize the window content

mysql版本升级+数据迁移

Circuit de fabrication manuelle d'un port série de niveau USB à TTL pour PL - 2303hx Old bear passing Sina blog

为什么Integer的比较最好使用equals

unsigned与signed之大白话

我的博客今天2岁167天了,我领取了先锋博主徽章_过路老熊_新浪博客

C# IO Stream 流(一)基础概念_基本定义

达梦数据库修改字段信息采坑记

使用百度地图API在地图中设置一个覆盖物(InfoWindow),可自定义窗口内容
随机推荐
Object类常用方法
mysql版本升级+数据迁移
关于二分和双指针的使用
C# IO Stream 流(二)扩展类_封装器
兆欧表电压档位选择_过路老熊_新浪博客
idea Kotlin版本升级
SSM integrated learning notes (mainly ideas)
【微信公众号H5】 生成带参数进入公众号关注页的二维码 监听用户关注公众号事件 自定义菜单栏 (服务端)
在win10下使用visual studio2015链接mysql数据库
Virtual and pure virtual destructors and their implementation in c++
登录拦截器
JS中常用知识点
PHP interprocess pass file descriptor
InputStream流已经关闭了,但是依旧无法delete文件或者文件夹,提示被JVM占用等
Understanding of pseudo classes
Transformation of communication protocol between Siemens S7-200PLC and Danfoss inverter_ Old bear passing by_ Sina blog
unsigned与signed之大白话
Efficacy of kiwi fruit enzyme_ Old bear passing by_ Sina blog
猕猴桃酵素的功效_过路老熊_新浪博客
谈一谈PHP变量或参数的Copy On Write机制