var a = 'a'; // 显式声明的全局变量 b = 'b'; // 隐式声明的全局变量 console.log(a); // a console.log(b); // b console.log(window.a); // a console.log(window.b); // b
// 显式声明的全局变量不能被删除 delete a; // 返回 false // 隐式声明的全局变量可以被删除 delete b; // 返回 true // 删除情况 console.log(typeof a); // string console.log(typeof b); // undefined
Object.getOwnPropertyDescriptor(window, a); // {value: "a", writable: true, enumerable: true, configurable: false}
Object.getOwnPropertyDescriptor(window, b); // {value: "b", writable: true, enumerable: true, configurable: true}
b = 'b'; var descriptor = Object.getOwnPropertyDescriptor(window, b); descriptor.configurable = false; Object.defineProperty(window, b, descriptor); delete b; // 返回 false
// 定义三个全局变量
var global_var = 1;
global_novar = 2; // 反面教材
(function () {
global_fromfunc = 3; // 反面教材
}());
// 试图删除
delete global_var; // false
delete global_novar; // true
delete global_fromfunc; // true
// 测试该删除
typeof global_var; // "number"
typeof global_novar; // "undefined"
typeof global_fromfunc; // "undefined"
var global = (function () {
return this;
}());
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有