在打字稿中删除键值对的正确方法是什么
What is the proper way to remove a key-value pair in typescript
我进行了以下实验,发现'delete'可以用来删除键值对。我的问题是:这是 'proper' 的方法吗?
let myMap:{[key:string]:string} = {};
myMap["hello"] = "world";
console.log("hello="+myMap["hello"]); // it prints 'hello=world'
delete myMap["hello"];
console.log("hello="+myMap["hello"]); // it prints 'hello=undefined'
My question is: is this the 'proper' way to do it?
这是正确的做法,但是还有 two caveats
- 除非属性是不可配置的
- 属性 是 继承的
例如 你不能删除 href
属性 of location
delete location.href //returns false since this property cannot be deleted
演示
var b = {
a: 1,
b: 2
};
Object.defineProperty(b, "c", {
enumerable: true,
configurable: false,
writable: true,
value: 3
});
delete b.c;
console.log(b); //all properties intact
我进行了以下实验,发现'delete'可以用来删除键值对。我的问题是:这是 'proper' 的方法吗?
let myMap:{[key:string]:string} = {};
myMap["hello"] = "world";
console.log("hello="+myMap["hello"]); // it prints 'hello=world'
delete myMap["hello"];
console.log("hello="+myMap["hello"]); // it prints 'hello=undefined'
My question is: is this the 'proper' way to do it?
这是正确的做法,但是还有 two caveats
- 除非属性是不可配置的
- 属性 是 继承的
例如 你不能删除 href
属性 of location
delete location.href //returns false since this property cannot be deleted
演示
var b = {
a: 1,
b: 2
};
Object.defineProperty(b, "c", {
enumerable: true,
configurable: false,
writable: true,
value: 3
});
delete b.c;
console.log(b); //all properties intact