1.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<script>
var name='武老板';
function func(){
var name='李杰';
function inner() {
alert(name);
}
return inner;
}
var ret=func();
ret()
</script>
</body>
</html>
测试结果:
2.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<script type="text/javascript">
function main() {
if (1 == 1) {
var name = '武老板';
}
console.log(name);
}
main()
</script>
</body>
</html>
测试结果:
没有打印出任何东西
3.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<script>
xo='alex';
function func() {
var xo = '武老板';
function inner() {
var xo='老男孩';
console.log(xo);
}
inner();
}
func();
</script>
</body>
</html>
测试结果:
没有打印出任何东西
4.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<script>
function Foo() {
console.log(xo);
var xo='武老板';
}
Foo();
</script>
</body>
</html>
结果:
没有打印出任何东西
5.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<script>
var name='alex';
function Foo() {
this.name='武老板';
this.func=function () {
alert(this.name);
}
}
var obj=new Foo();
obj.func()
</script>
</body>
</html>
结果:
弹框
6.重点题目(详细了解that和this的区别)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<script>
var name='alex';
function Foo() {
this.name='武老板';
this.func=function () {
//注意这里是自执行函数,这里的this指的是上边的function结果自然是alex了
(function() {
alert(this.name);
})()
}
}
var obj=new Foo();
obj.func()
</script>
</body>
</html>
结果: