当前位置:网站首页>JS ES5也可以创建常量?
JS ES5也可以创建常量?
2022-06-30 15:47:00 【InfoQ】
本文简介
ES6letconstES6constES6ES 5 创建常量
Object.defineProperty
的基础用法
ES6constObject.definePropertyVue2Object.definePropertyObject.definePropertyObject.definePropertyObject.defineProperty(obj, prop, descriptor)- obj: 要定义属性的对象。
- prop: 要定义或修改的属性的名称或
Symbol。
- descriptor: 要定义或修改的属性描述符。
var LH = {}
Object.defineProperty(LH, 'name', {
value: '雷猴'
})
console.log(LH) // 输出: {name: '雷猴'}
LH.name = '鲨鱼辣椒'
console.log(LH) // 输出: {name: '雷猴'}
LH.namedescriptorvaluewritablefalseLH.name = '鲨鱼辣椒'writabletruevar LH = {}
Object.defineProperty(LH, 'name', {
value: '雷猴',
writable: true // 允许修改
})
console.log(LH) // 输出: {name: '雷猴'}
LH.name = '鲨鱼辣椒'
console.log(LH) // 输出: {name: '鲨鱼辣椒'}
创建常量
LHwindowwindowObject.defineProperty(window, 'NAME', {
value: '雷猴'
})
console.log(NAME) // 输出: 雷猴
NAME = '鲨鱼辣椒'
console.log(NAME) // 输出: 雷猴
window.NAME = '蟑螂恶霸'
console.log(NAME) // 输出: 雷猴
NAME常量居然可以修改值?
valueObject.defineProperty(window, 'NAME', {
value: {
nickname: '雷猴'
}
})
console.log(NAME)
NAME.nickname = '鲨鱼辣椒'
console.log(NAME)
兼容性
Object.defineProperty
边栏推荐
- How the edge computing platform helps the development of the Internet of things
- [algorithm] after summarizing the four linked lists, I brushed two interview questions
- 牛客网:有多少个不同的二叉搜索树
- BC1.2 PD协议
- Create statement for Oracle export view
- Policy Center-User Data
- 招标公告:2022年台州联通Oracle一体机和数据库维保服务项目
- 24:第三章:开发通行证服务:7:自定义异常(来表征程序中出现的错误);创建GraceExceptionHandler来全局统一处理异常(根据异常信息,构建对应的API统一返回对象的,JSON数据);
- MC Instruction Decoder
- Two methods for MySQL to open remote connection permission
猜你喜欢

The new tea drinks are "dead and alive", but the suppliers are "full of pots and bowls"?

【牛客网刷题系列 之 Verilog快速入门】~ 位拆分与运算

RT thread heap size setting

topic: Privacy, Deception and Device Abuse

I implement "stack" with C I

The image variables in the Halcon variable window are not displayed, and it is useless to restart the software and the computer

Tencent two sides: @bean and @component are used on the same class. What happens?

Implementation of Devops in the core field of qunar, the Internet R & D Efficiency

Hundreds of lines of code to implement a JSON parser

抖快B为啥做不好综艺
随机推荐
The inspiration from infant cognitive learning may be the key to the next generation of unsupervised machine learning
I implement "stack" with C I
云和恩墨中标天津滨海农村商业银行2022-2023年度Oracle维保项目
Policy Center > Device and Network Abuse
[BJDCTF2020]The mystery of ip|[CISCN2019 华东南赛区]Web11|SSTI注入
Tencent two sides: @bean and @component are used on the same class. What happens?
详解Go语言中for循环,break和continue的使用
register_chrdev和cdev_init cdev_add用法区别
19:00 p.m. tonight, knowledge empowerment phase 2 live broadcast - control panel interface design of openharmony smart home project
备战数学建模35-时间序列预测模型
RT-Thread 堆区大小设置
How the edge computing platform helps the development of the Internet of things
Bidding announcement: Tianjin housing provident fund management center database all-in-one machine and database software project (budget: 6.45 million)
15年做糊21款硬件,谷歌到底栽在哪儿?
实时渲染和预渲染有什么区别
360数科、蚂蚁集团等入选中国信通院“业务安全推进计划”成员单位
Installing jupyter notebook under Anaconda
中航无人机科创板上市:市值385亿 拳头产品是翼龙无人机
Dart: string replace related methods to solve replacement characters
The image variables in the Halcon variable window are not displayed, and it is useless to restart the software and the computer