1.javascript
<!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>
測試結果:html
2.java
<!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>
測試結果:this
沒有打印出任何東西spa
4.code
<!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>
結果:htm
沒有打印出任何東西blog
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>
結果: