delete运算符是一个一元运算符,它将删除运算数所指定的对象的属性、数组元素或变量。如果删除操作成功,它将返回true,如果运算数不能被删除,它将返回false。注意删除属性、变量或数组元素不只是把它们的值设置为undefined。当输出一个属性后,该属性不再存在。并非所有的属性和变量都是可以删除的,某些内部的核心属性和客户端属性不能删除,用var语句生命的用户定义变量也不能被删除。如果delete使用的运算数是一个不存在的属性,它将返回true。
var o = {x:1 , y:2};//Defined a variable;initialize to object
detele o.x; //Delete one of the other properties;return true
typeof o.x; //property does not exist;return "undefined"
delete o.x; //Delete a nonexistent property;return true
delete o; //Can't delete a declared variable;return false
delete 1; //Can't delete an integer;return true
x = 1; //Implicity declared a variable without var keyword
delete x; //Can delete this kind of variable;return true
x; //Runtime error:x is not defined
delete所能影响的只是属性值,并不能影响被这些属性引用的对象:
var my = new Object(); //create an object named "my"
my.hire = new Date(); //my.hire refers to a Date objct
my.fire = my.hire; //my.fire refers to the same object
delete my.hire; //hire property is deleted;return true
docunment.write(my.fire); //But my.fire still refers to the Date object