假设我们有一个对象,如下所示:
const myObject = {
"ircEvent": "PRIVMSG",
"method": "newURI",
"regex": "^http://.*"
};我们需要说明删除属性正则表达式以使用新的myObject的最佳方法吗?
以下是解决方案-
const myObject = {
"ircEvent": "PRIVMSG",
"method": "newURI"
};delete运算符用于从对象中删除属性。
const myObject = {
"ircEvent": "PRIVMSG",
"method": "newURI",
"regex": "^http://.*"
};
delete myObject['regex'];
console.log(myObject.hasOwnProperty("regex")); // falseJavaScript中的delete运算符与C和C ++中的关键字具有不同的功能-
它不会直接释放内存。相反,其唯一目的是从对象中删除属性。
输出结果
以下是控制台输出-
False