var scope = "global ";
function checkScope() {
var scope = "local ";
function childCheck() {
var scope = "childLocal ";
document.write(scope);
}
function childUndefined() {
document.write(scope);
var scope;
}
function childOverride() {
scope = "childOverride ";
document.write(scope);
}
document.write(scope); //输出"local"
childCheck(); //输出"childLocal"
childUndefined(); //输出"undefined"
childOverride(); //输出"childOverride"
document.write(scope); //输出"childOverride"
}
checkScope(); //输出"local childLocal undefinedchildOverride childOverride"
document.write(scope); //输出"global "
var scope = "global";
function checkScope() {
var scope = "local";
document.write(scope);
}
checkScope(); //输出"local"
document.write(scope); //输出"global"
var scope = "global";
function checkScope() {
scope = "local";
document.write(scope);
}
checkScope(); //输出"local"
document.write(scope); //输出"local"
var scope = "global";
function checkScope() {
document.write(scope); //语句4.1
var scope = "local"; //语句4.2
document.write(scope);
}
checkScope(); //输出"undefinedlocal"
var scope = "global ";
var obj = new Object();
obj.scope = "object ";
obj.checkScope = function () {
var scope = "loacl ";
document.write(scope); //输出"loacl"
document.write(this.scope); //输出"object"
document.write(window.scope); //输出"global"
}
obj.checkScope(); //输出"loacl object global"
var foo = 'hello';
var sayHello = function() {
console.log(foo);
};
sayHello(); // logs 'hello'
console.log(foo); // also logs 'hello'
var sayHello = function() {
var foo = 'hello';
console.log(foo);
};
sayHello(); // logs 'hello'
console.log(foo); // doesn't log anything
var foo = 'world';
var sayHello = function() {
var foo = 'hello';
console.log(foo);
};
sayHello(); // logs 'hello'
console.log(foo); // logs 'world'
var myFunction = function() {
var foo = 'hello';
var myFn = function() {
console.log(foo);
};
foo = 'world';
return myFn;
};
var f = myFunction();
f(); // logs 'world' -- haha
// 一个自执行的匿名函数
(function() {
var baz = 1;
var bim = function() { alert(baz); };
bar = function() { alert(baz); };
})();
console.log(baz); // 在函数外面不能访问 baz
bar(); // 声明 bar 的时候并没有用 var
// 所以 bar 是一个全局变量; 但是,
// bar 和 baz 在相同的作用域内被定义,
// 所以 bar 可以访问 baz
// 其实 bar 是个闭包函数
bim(); // bim 的作用域只限于匿名函数内部,
// 所以这里不能调用
var foo = 'hello';
var sayHello = function() {
console.log(foo);
};
sayHello(); // logs 'hello'
console.log(foo); // also logs 'hello'
var sayHello = function() {
var foo = 'hello';
console.log(foo);
};
sayHello(); // logs 'hello'
console.log(foo); // doesn't log anything
var foo = 'world';
var sayHello = function() {
var foo = 'hello';
console.log(foo);
};
sayHello(); // logs 'hello'
console.log(foo); // logs 'world'
var myFunction = function() {
var foo = 'hello';
var myFn = function() {
console.log(foo);
};
foo = 'world';
return myFn;
};
var f = myFunction();
f(); // logs 'world' -- haha
// 一个自执行的匿名函数
(function() {
var baz = 1;
var bim = function() { alert(baz); };
bar = function() { alert(baz); };
})();
console.log(baz); // 在函数外面不能访问 baz
bar(); // 声明 bar 的时候并没有用 var
// 所以 bar 是一个全局变量; 但是,
// bar 和 baz 在相同的作用域内被定义,
// 所以 bar 可以访问 baz
// 其实 bar 是个闭包函数
bim(); // bim 的作用域只限于匿名函数内部,
// 所以这里不能调用
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有