สมมติว่าเรามีอ็อบเจกต์ดังนี้:
const myObject = {
"ircEvent": "PRIVMSG",
"method": "newURI",
"regex": "^https://.*"
};เราต้องการลบ property regex ออก วิธีที่ดีที่สุดคือใช้ delete operator
const myObject = {
"ircEvent": "PRIVMSG",
"method": "newURI"
};delete operator ใช้ลบ property จากอ็อบเจกต์
const myObject = {
"ircEvent": "PRIVMSG",
"method": "newURI",
"regex": "^https://.*"
};
delete myObject['regex'];
console.log(myObject.hasOwnProperty("regex")); // falsedelete operator ใน JS ทำงานต่างจาก C/C++ - มันไม่ได้ free memory โดยตรง แต่เพื่อลบ property จากอ็อบเจกต์เท่านั้น
ผลลัพธ์
ผลลัพธ์ในคอนโซล:
False