04.练习一.html 1016 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>Document</title>
  8. </head>
  9. <body>
  10. <script>
  11. //代码片段一
  12. var name = "The Window"
  13. var object = {
  14. name : "My Object",
  15. getNameFunc : function(){
  16. return function(){
  17. return this.name //此处的this是window
  18. }
  19. }
  20. }
  21. alert(object.getNameFunc()()) //The Window
  22. //代码片段二
  23. var name2 = "The Window"
  24. var object2 = {
  25. name2 : "My Object",
  26. getNameFunc : function(){
  27. var that = this
  28. return function(){
  29. return that.name2 //此处的that是object2
  30. }
  31. }
  32. }
  33. alert(object2.getNameFunc()()) //My Object
  34. </script>
  35. </body>
  36. </html>