如何使用 JavaScript 中的 delete 操作符来删除一个 setter?


若要使用 delete 操作符删除一个 setter,请使用 delete 关键字。下面是删除的方法 -

delete obj.name

示例

您可以尝试运行以下代码来了解如何删除一个 setter

演示

<html>
   <body>
      <script>
         var department = {
            deptName: "Marketing",
            deptZone: "North",
            deptID: 101,
            get details() {
               return "Department Details<br>" + "Name: " + this.deptName + " <br>Zone: " + this.deptZone + "<br>ID: " + this.deptID;
            },
            set details(info) {
               var res = info.toString().split(' ');
               this.deptName = res[0] || '';
               this.deptZone = res[1] || '';
               this.deptID = res[2] || '';
            }
         }
         department.details = 'Marketing North 001';
         document.write("<br>Department Name: " + department.deptName);
         document.write("<br>Departmet Zone: " + department.deptZone);
         document.write("<br>Department ID: " + department.deptID);
         delete department.deptName;
         document.write("<br>Department Name (after deletion): " + department.deptName);
      </script>
   </body>
</html>

更新于:16-Jun-2020

183 次浏览

开启你的职业生涯

通过完成课程获得认证

开始
广告