查看“Vue Object.defineProperty”的源代码
←
Vue Object.defineProperty
跳到导航
跳到搜索
因为以下原因,您没有权限编辑本页:
您所请求的操作仅限于该用户组的用户使用:
用户
您可以查看和复制此页面的源代码。
https://www.bilibili.com/video/BV1Zy4y1K7SH?p=11 === ES6 往对象上添加属性 === <syntaxhighlight lang="html"> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>回顾Object.defineProperty方法</title> </head> <body> <script type="text/javascript"> let person = { name:'张三', sex:'男' } Object.defineProperty(person, 'age', { value:18 }) console.log(person); </script> </body> </html> </syntaxhighlight> === 通过 Object.defineProperty 添加的属性遍历不到 === <syntaxhighlight lang="html"> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>回顾Object.defineProperty方法</title> </head> <body> <script type="text/javascript"> let person = { name:'张三', sex:'男', // age:18 } Object.defineProperty(person, 'age', { value:18 }) console.log(Object.keys(person)); console.log(person); </script> </body> </html> </syntaxhighlight>证明通过 Object.defineProperty 添加的属性不可枚举<syntaxhighlight lang="html"> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>回顾Object.defineProperty方法</title> </head> <body> <script type="text/javascript"> let person = { name:'张三', sex:'男', // age:18 } Object.defineProperty(person, 'age', { value:18 }) //console.log(Object.keys(person)); for (let key in person) { console.log('@', person[key]); } console.log(person); </script> </body> </html> </syntaxhighlight> === 通过 enumerable:true 让 Object.defineProperty 添加的属性可枚举 === <syntaxhighlight lang="html"> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>回顾Object.defineProperty方法</title> </head> <body> <script type="text/javascript"> let person = { name:'张三', sex:'男', // age:18 } Object.defineProperty(person, 'age', { value:18, enumerable:true //控制属性是否可以枚举,默认值是 false }) //console.log(Object.keys(person)); for (let key in person) { console.log('@', person[key]); } console.log(person); </script> </body> </html> </syntaxhighlight> === 可以在 console 里改变 let 声明的对象属性值 === <syntaxhighlight lang="html"> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>回顾Object.defineProperty方法</title> </head> <body> <script type="text/javascript"> let person = { name:'张三', sex:'男', age:18 } // Object.defineProperty(person, 'age', { // value:18, // enumerable:true, //控制属性是否可以枚举,默认值是 false // }) //console.log(Object.keys(person)); for (let key in person) { console.log('@', person[key]); } console.log(person); </script> </body> </html> </syntaxhighlight>在 console 里通过 person.age=19 给 person.age 赋新值,然后再在 console 中通过 person.age 输出,发现值改变了 === 通过 Object.defineProperty 给对象添加的属性值无法被改变 ===
返回至
Vue Object.defineProperty
。
导航菜单
个人工具
登录
名字空间
页面
讨论
变种
视图
阅读
查看源代码
查看历史
更多
搜索
导航
首页
Spring Boot 2 零基础入门
Spring Cloud
Spring Boot
设计模式之禅
VUE
Vuex
Maven
算法
技能树
Wireshark
IntelliJ IDEA
ElasticSearch
VirtualBox
软考
正则表达式
程序员精讲
软件设计师精讲
初级程序员 历年真题
C
SQL
Java
FFmpeg
Redis
Kafka
MySQL
Spring
Docker
JMeter
Apache
Linux
Windows
Git
ZooKeeper
设计模式
Python
MyBatis
软件
数学
PHP
IntelliJ IDEA
CS基础知识
网络
项目
未分类
MediaWiki
镜像
问题
健身
国债
英语
烹饪
常见术语
MediaWiki帮助
工具
链入页面
相关更改
特殊页面
页面信息