练习题2_讲解.html 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>Document</title>
  7. </head>
  8. <body>
  9. <div id="div">hello</div>
  10. <script>
  11. // var x = 10;
  12. // function test() {
  13. // this.x = 20
  14. // console.log(this.x)//20
  15. // }
  16. // test()
  17. // 第二题
  18. // var name = "window"
  19. // var obj = {
  20. // name: "obj",
  21. // func1: function () {
  22. // console.log(this.name);//obj
  23. // (function () {
  24. // console.log(this.name)//window
  25. // })()
  26. // }
  27. // }
  28. // obj.func1()
  29. // 第三题
  30. // var name = "the window";
  31. // var object = {
  32. // name: "My Object",
  33. // getName: function () {
  34. // return this.name;
  35. // }
  36. // }
  37. // console.log(object.getName());//My Object
  38. // console.log((object.getName)());//My Object
  39. // console.log((object.getName = object.getName)());//the window
  40. // var a = 20;
  41. // var obj = {
  42. // a:10,
  43. // foo:function(){
  44. // console.log(this.a)
  45. // }
  46. // }
  47. // (function(){
  48. // console.log(this.a)
  49. // }())
  50. // var a = 1;
  51. // if(1){}
  52. // var foo = obj.foo;
  53. // obj.foo();
  54. // foo();
  55. // var foo = function(){
  56. // console.log(123);
  57. // }
  58. // (function(){console.log(123);}())
  59. // (function(){
  60. // console.log(123);
  61. // }());
  62. // (function(){
  63. // console.log(123);
  64. // })();
  65. // 第四题
  66. // document.getElementById("div").onclick = function(){
  67. // console.log(this.innerText);
  68. // }
  69. // 第五题
  70. // var name = "window"
  71. // var obj = {
  72. // name: "obj"
  73. // }
  74. // window.setInterval(function () {
  75. // console.log(this.name)
  76. // }, 300)
  77. // window.setInterval(function () {
  78. // console.log(this.name)
  79. // }.call(obj), 300)
  80. // function foo(){
  81. // console.log("hello");
  82. // }
  83. // console.log(foo());
  84. // setInterval(foo.call(window),1000)
  85. // 第六题
  86. // function foo() {
  87. // //补全此处代码实现每隔一秒输出 hello world
  88. // // console.log("hello world");
  89. // return function(){
  90. // console.log("hello world");
  91. // }
  92. // }
  93. // window.setInterval(foo(), 1000);
  94. // function foo() {
  95. // //补全此处代码实现每隔一秒 输出累加的值 1 2 3 4 5 6 ...
  96. // var i = 1
  97. // return function(){
  98. // console.log(i++);
  99. // }
  100. // }
  101. // window.setInterval(foo(), 1000);
  102. // 7、补全下列代码实现 1+2+3+4 (this 不能指向window)
  103. // function add(c, d) {
  104. // return this.a + this.b + c + d;
  105. // }
  106. /*
  107. 在此补全代码 以两种以上方法实现
  108. */
  109. // var a = 1;
  110. // var b = 2;
  111. // var obj = {
  112. // a:1,
  113. // b:2
  114. // }
  115. // var res = add(3,4)
  116. // var res = add.call(obj,3,4);
  117. // console.log(res);
  118. // 第八题
  119. // function f() {
  120. // return this.a;
  121. // }
  122. // // bind不可以对同一个函数绑定多次 如果绑定多次的话以第一次为准
  123. // var g = f.bind({ a: "azerty" });
  124. // console.log(g());//azerty
  125. // var h = g.bind({ a: 'yoo' });
  126. // console.log(h());//azerty
  127. // var o = {
  128. // a: 'loveCoding',
  129. // f: function(){
  130. // return this.a
  131. // },
  132. // g: g,
  133. // h: h
  134. // };
  135. // console.log(o.f(), o.g(), o.h());//loveCoding azerty azerty
  136. // var a = "hello"
  137. // function foo(i){
  138. // console.log(this.a+i);//10world
  139. // }
  140. // var obj = {
  141. // a:10
  142. // }
  143. // foo()
  144. // foo.call(obj,"world");
  145. // foo.apply(obj,["world"]);
  146. // var foo2 = foo.bind(obj,"world");
  147. // foo2()
  148. //9、补全下列代码
  149. var o = { prop: 'loveCoding' };
  150. function independent() {
  151. return this.prop;
  152. }
  153. //在此补全代码
  154. // o.f = independent.bind(o);
  155. o.f = function(){
  156. return independent.call(o)
  157. }
  158. console.log(o.f()); // loveCoding
  159. </script>
  160. </body>
  161. </html>