网站首页 > 技术文章 正文
使用 Object.defineProperty 方法在对象上定义一个新属性或修改现有属性。
const def = (obj, key, value, writable = false) => {
Object.defineProperty(obj, key, {
configurable: true,
enumerable: false,
writable,
value
});
};
// 示例对象
const person = {};
// 使用 def 函数定义属性
def(person, 'name', 'John', true);
def(person, 'age', 30);
// 访问属性
console.log(person.name); // 输出: John
console.log(person.age); // 输出: 30
// 尝试枚举属性
for (let key in person) {
console.log(key); // 不会输出 'name' 和 'age',因为 enumerable: false
}
// 修改属性
person.name = 'Jane';
console.log(person.name); // 输出: Jane
// 尝试删除属性
delete person.name;
console.log(person.name); // 输出: undefined,因为 configurable: true
- 定义属性:使用 Object.defineProperty 方法在对象 person 上定义了 name 和 age 属性。
- 属性描述符:configurable: true 允许属性被删除或修改,enumerable: false 使属性在枚举时不可见,writable 决定属性是否可写,value 设置属性的值。
- 访问和修改属性:可以访问和修改属性 name,因为 writable 为 true。
- 枚举属性:由于 enumerable: false,属性 name 和 age 不会出现在枚举中。
- 删除属性:由于 configurable: true,属性 name 可以被删除。
Object.defineProperty 可以用于 HTML 元素(如 div)来定义或修改属性。以下是一个示例,展示如何使用 Object.defineProperty 给 div 元素定义一个新属性:
// 创建一个 div 元素
const div = document.createElement('div');
// 使用 Object.defineProperty 定义一个新属性
Object.defineProperty(div, 'customProperty', {
value: 'Hello, World!',
writable: true,
enumerable: true,
configurable: true
});
// 访问新定义的属性
console.log(div.customProperty); // 输出: Hello, World!
// 修改新定义的属性
div.customProperty = 'Hello, JavaScript!';
console.log(div.customProperty); // 输出: Hello, JavaScript!
// 枚举属性
for (let key in div) {
if (div.hasOwnProperty(key)) {
console.log(key); // 输出: customProperty
}
}
// 删除属性
delete div.customProperty;
console.log(div.customProperty); // 输出: undefined
- 创建元素:使用 document.createElement('div') 创建一个 div 元素。
- 定义属性:使用 Object.defineProperty 方法在 div 元素上定义一个名为 customProperty 的新属性,并设置其值为 'Hello, World!'。属性描述符设置为 writable: true、enumerable: true 和 configurable: true。
- 访问和修改属性:可以访问和修改新定义的属性 customProperty。
- 枚举属性:由于 enumerable: true,属性 customProperty 会出现在 for...in 循环中。
- 删除属性:由于 configurable: true,属性 customProperty 可以被删除。
那么Object.defineProperty定义的属性,与div.customProperty赋值的属性,有什么区别?
属性描述符
- 直接赋值:
- 默认情况下,属性是可枚举的(enumerable: true)、可配置的(configurable: true)和可写的(writable: true)。
const div = document.createElement('div');
div.customProperty = 'Hello, World!';
console.log(div.customProperty); // 输出: Hello, World!
Object.defineProperty:
- 允许你精确控制属性的行为,包括是否可枚举、可配置和可写。
- 示例:
const div = document.createElement('div');
Object.defineProperty(div, 'customProperty', {
value: 'Hello, World!',
writable: false,
enumerable: false,
configurable: false
});
console.log(div.customProperty); // 输出: Hello, World!
可枚举性
- 直接赋值:
- 属性是可枚举的,可以在 for...in 循环和 Object.keys 中看到。
- 示例:
const div = document.createElement('div');
div.customProperty = 'Hello, World!';
for (let key in div) {
console.log(key); // 输出: customProperty
}
Object.defineProperty:
- 你可以控制属性是否可枚举。
const div = document.createElement('div');
Object.defineProperty(div, 'customProperty', {
value: 'Hello, World!',
enumerable: false
});
for (let key in div) {
console.log(key); // 不会输出 customProperty
}
可写性
- 直接赋值:
- 属性默认是可写的。
const div = document.createElement('div');
div.customProperty = 'Hello, World!';
div.customProperty = 'New Value';
console.log(div.customProperty); // 输出: New Value
Object.defineProperty:
- 你可以控制属性是否可写。
const div = document.createElement('div');
Object.defineProperty(div, 'customProperty', {
value: 'Hello, World!',
writable: false
});
div.customProperty = 'New Value';
console.log(div.customProperty); // 输出: Hello,
说白了,直接赋值属性,就是Object.defineProperty定义属性的简化版本。
猜你喜欢
- 2024-10-04 Vue选项:computed计算属性(vue3.0 计算属性)
- 2024-10-04 Vue 中的计算属性(vue中的计算属性)
- 2024-10-04 Js基础7:表单元素属性(js中表单)
- 2024-10-04 Jquery属性——学习笔记(一)(如何使用jquery设置一个属性值)
- 2024-10-04 前端入门——在网页中使用JavaScript
- 2024-10-04 前端-JavaScript中的class(javascript class)
- 2024-10-04 如何获取JavaScript中某个函数可接受的参数数量
- 2024-10-04 关于js设置对象attr属性的问题(js对象属性值)
- 2024-10-04 react三大核心属性(react的核心)
- 2024-10-04 JS 对象基本用法(js对象的操作方法)
- 10-02基于深度学习的铸件缺陷检测_如何控制和检测铸件缺陷?有缺陷铸件如何处置?
- 10-02Linux Mint 22.1 Cinnamon Edition 搭建深度学习环境
- 10-02AWD-LSTM语言模型是如何实现的_lstm语言模型
- 10-02NVIDIA Jetson Nano 2GB 系列文章(53):TAO模型训练工具简介
- 10-02使用ONNX和Torchscript加快推理速度的测试
- 10-02tensorflow GPU环境安装踩坑日记_tensorflow配置gpu环境
- 10-02Keye-VL-1.5-8B 快手 Keye-VL— 腾讯云两卡 32GB GPU保姆级部署指南
- 10-02Gateway_gateways
- 最近发表
-
- 基于深度学习的铸件缺陷检测_如何控制和检测铸件缺陷?有缺陷铸件如何处置?
- Linux Mint 22.1 Cinnamon Edition 搭建深度学习环境
- AWD-LSTM语言模型是如何实现的_lstm语言模型
- NVIDIA Jetson Nano 2GB 系列文章(53):TAO模型训练工具简介
- 使用ONNX和Torchscript加快推理速度的测试
- tensorflow GPU环境安装踩坑日记_tensorflow配置gpu环境
- Keye-VL-1.5-8B 快手 Keye-VL— 腾讯云两卡 32GB GPU保姆级部署指南
- Gateway_gateways
- Coze开源本地部署教程_开源canopen
- 扣子开源本地部署教程 丨Coze智能体小白喂饭级指南
- 标签列表
-
- cmd/c (90)
- c++中::是什么意思 (84)
- 标签用于 (71)
- 主键只能有一个吗 (77)
- c#console.writeline不显示 (95)
- pythoncase语句 (88)
- es6includes (74)
- sqlset (76)
- apt-getinstall-y (100)
- node_modules怎么生成 (87)
- chromepost (71)
- flexdirection (73)
- c++int转char (80)
- mysqlany_value (79)
- static函数和普通函数 (84)
- el-date-picker开始日期早于结束日期 (76)
- js判断是否是json字符串 (75)
- c语言min函数头文件 (77)
- asynccallback (87)
- localstorage.removeitem (74)
- vector线程安全吗 (70)
- java (73)
- js数组插入 (83)
- mac安装java (72)
- 无效的列索引 (74)