显式声明变量和隐式声明变量的区别

122 阅读1分钟

区别1:在函数中,显式声明变量式局部变量,隐式变量 式全局变量;
区别2:隐式声明的变量可以被删除,显式变量不能删除;
区别3:隐式声明变量的默认配置configurble为true,显式声明变量的默认配置configurable为false;

var abc = 123;// 显式声明变量
str = 'abc';// 隐式声明变量

console.log( window.abc );// 123
console.log( window.str );// 'abc'

delete window.abc;
delete window.str;

console.log( window.abc );// 123
console.log( window.str );// undefined

var abcDesc = Object.getOwnPropertyDescriptor(window,'abc');
var strDesc = Object.getOwnPropertyDescriptor(window,'str');
console.log( abcDesc );
console.log( strDesc );