使用JavaScript中的delete属性,可以从对象中删除属性。您可以尝试运行以下代码,以了解如何使用Delete运算符。在这里,我们要删除图书价格-
<html>
<head>
<title>JavaScript Delete Operator</title>
<script>
function book(title, author) {
this.title = title;
this.author = author;
}
</script>
</head>
<body>
<script>
var myBook = new book("WordPress Development", "Amit");
book.prototype.price = null;
myBook.price = 500;
document.write("<h2>Details before deleting book price</h2>");
document.write("Book title is : " + myBook.title + "<br>");
document.write("Book author is : " + myBook.author + "<br>");
document.write("Book price is : " + myBook.price);
delete myBook.price;
document.write("<br><h2>Details after deleting book price</h2>");
document.write("Book title is : " + myBook.title + "<br>");
document.write("Book author is : " + myBook.author + "<br>");
document.write("Book price is : " + myBook.price + "<br>");
</script>
</body>
</html>