黄子涵

4.44 delete 运算符

delete 是用于删除属性的单目运算符,其功能为从对象中删除以操作数指定的属性。delete 运算的运算结果为布尔值。如果属性被删除,或所要删除的属性不存在,则结果为真,否则结果为假。

var hzh = {
    name: "黄子涵",
    tall: '177cm',
    tel: 19124896017,
    brother: "黄春钦",
    mother: '陈兰英',
    father: '黄开卓'
}

delete hzh.father;
console.log("是否已经删除father属性:");
console.log(delete father);
console.log("");
console.log("看能不能访问father属性:");
console.log(hzh.father);
[Running] node "e:\HMV\JavaScript\JavaScript.js"
是否已经删除father属性:
true

看能不能访问father属性:
undefined

[Done] exited with code=0 in 1.877 seconds

在 JavaScript 中,从内部结构来看,全局变量是全局对象的属性。不过如果 delete 运算的操作数是一个全局变量,情况则有些特殊。

通过 var 声明的全局变量是无法被 delete 的,而没用使用 var 声明的隐式的全局变量则可以被 delete。不推荐使用隐式的全局变量。因此,原则上不应对全局
变量进行 delete 运算。

var hzh = {
    name: "黄子涵",
    tall: '177cm',
    tel: 19124896017,
    brother: "黄春钦",
    mother: '陈兰英',
    father: '黄开卓'
}

delete hzh;
console.log("是否已经删除全局变量hzh:");
console.log(delete hzh);
console.log("");
console.log("看能不能访问全局变量hzh:");
console.log(hzh);
[Running] node "e:\HMV\JavaScript\JavaScript.js"
是否已经删除全局变量hzh:
false

看能不能访问全局变量hzh:
{
  name: '黄子涵',
  tall: '177cm',
  tel: 19124896017,
  brother: '黄春钦',
  mother: '陈兰英',
  father: '黄开卓'
}

[Done] exited with code=0 in 0.6 seconds
posted @ 2022-05-30 10:03  黄子涵  阅读(104)  评论(0编辑  收藏  举报