先来抛一个例子
var a = 1;
b = 2;
function fn(){
c = 3;
}
fn()
console.log(a) //1
console.log(b) //2
console.log(c) //3
delete a;
delete b;
delete c;
console.log(typeof a) //number
console.log(typeof b) //undefined
console.log(typeof c) //undefined
说明:在全局作用域中使用var创建的全局变量是不能被delete删除的
可以删除对象的某个属性:
var o = { x: 1 };
delete o.x; // true
o.x; // undefined
但不能删除变量,比如以下面这种方式声明的:
var x = 1;
delete x; // false
x; // 1
也不能删除函数,比如下面所定义的:
function x(){}
delete x; // false
typeof x; // "function"
删除不了原型链中的变量
fun.prototype.age = 18;
delete obj.age;
console.log(obj.age) //18