实现说明
顶层就这几处状态:options、patch、items。
一个对象字面量承载 retries、timeout、verbose——每个值都就地写死。
使用要点
- 演示输出是带标签的(
verbose own:、verbose in:、value:),每个操作都会留下自己的一行标注。
常见问题
在一个没有任何依赖声明的空项目里能跑吗?
可以——空项目原样能跑,因为什么都不用声明。 全部导入都来自标准库:present with value undefined。 跨调用保留的状态放在这些顶层绑定里:options、patch、items。
核心语句
const options = { retries: 3, timeout: 1500, verbose: true };
const patch = { debug: false };
const items = ['a', 'b', 'c'];
代码示例
const options = { retries: 3, timeout: 1500, verbose: true };
// delete removes an own property; afterwards both in and hasOwn miss it
delete options.verbose;
console.log('verbose own:', Object.hasOwn(options, 'verbose'));
console.log('verbose in:', 'verbose' in options);
// reading a deleted property yields undefined, but that differs from "present with value undefined"
console.log('value:', options.verbose);
delete options.retries;
console.log('after delete:', JSON.stringify(options));
// assigning undefined does not remove the key: the key stays on the object
const patch = { debug: false };
patch.debug = undefined;
console.log('still own:', Object.hasOwn(patch, 'debug'));
console.log('keys:', Object.keys(patch).length);
// delete on an array leaves holes: the index property disappears while length is unchanged
const items = ['a', 'b', 'c'];
delete items[1];
console.log('index 1 present:', 1 in items);
console.log('length unchanged:', items.length);