练习题3_讲解.html 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. <script>
  10. //1、写出下列输出结果
  11. // var x = 10;
  12. // function test() {
  13. // var x = 20
  14. // console.log(this.x)//10
  15. // }
  16. // test()
  17. //2、写出下列输出结果
  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. //3、写出下列结果
  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. // console.log(object.getName);
  41. // let foo = function () {
  42. // return this.name;
  43. // };
  44. // console.log(foo());
  45. // var a = 10;
  46. // var b = 20;
  47. // if(a = b){
  48. // console.log("true");
  49. // }
  50. //4、下列代码中当div的点击事件触发时输出的结果是?
  51. // document.getElementById("div").onclick = function () {
  52. // console.log(this)//div
  53. // };
  54. //5、请写出下列代码运行结果
  55. // var name = "window"
  56. // var obj = {
  57. // name: "obj"
  58. // }
  59. // setInterval(function () {
  60. // console.log(this.name)//每隔300ms输出window
  61. // }, 300)
  62. // setInterval(function () {
  63. // console.log(this.name)//输出一次obj
  64. // }.call(obj), 300)
  65. //6、请补全下列代码
  66. // function foo() {
  67. // //补全此处代码实现每隔一秒输出 hello world
  68. // // console.log("hello world");
  69. // return function(){
  70. // console.log("hello world");
  71. // }
  72. // }
  73. // window.setInterval(foo(), 1000);
  74. // 7、补全下列代码实现 1+2+3+4
  75. function add(c, d) {
  76. return this.a + this.b + c + d;
  77. }
  78. // var obj = {
  79. // a: 1,
  80. // b: 2
  81. // }
  82. // var res = add.call(obj, 3, 4);
  83. // var a = 1;
  84. // var b = 2;
  85. // var res = add(3,4);
  86. // console.log(res);//10
  87. //8、写出下列输出结果
  88. // function f() {
  89. // return this.a;
  90. // }
  91. // // bind 方法只能绑定一次 不能再次绑定 如果出现多次绑定 以最第一次绑定为准
  92. // var g = f.bind({ a: "azerty" });
  93. // console.log(g());//azerty
  94. // var h = g.bind({ a: 'yoo' });
  95. // console.log(h());//azerty
  96. // var o = { a: 'loveCoding', f: f, g: g, h: h };
  97. // console.log(o.f(), o.g(), o.h());//loveCoding azerty azerty
  98. //9、补全下列代码
  99. // var o = { prop: 'loveCoding' };
  100. // function independent() {
  101. // return this.prop;
  102. // }
  103. // //在此补全代码
  104. // o.f = independent;
  105. // console.log(o.f()); // loveCoding
  106. //10、用call 或 apply 实现bind 方法
  107. function foo(i,j) {
  108. console.log(this.a,i,j)
  109. }
  110. // function fo2(){
  111. // console.log(this.a);
  112. // }
  113. var obj = {
  114. a: "hello"
  115. }
  116. // 在函数对象(Function)下添加一个bind2方法
  117. Function.prototype.bind2 = function(context){
  118. let _this = this;
  119. // console.log(arguments);
  120. // let arg = Array.from(arguments).slice(1);
  121. var arg = Array.prototype.slice.call(arguments,1);
  122. // console.log(arg);
  123. return function(){
  124. // _this.call(context,...arg);
  125. _this.apply(context,arg);
  126. }
  127. }
  128. var foo2 = foo.bind2(obj,1,2);
  129. // var foo3 = fo2.bind2(obj);
  130. foo2()
  131. // foo3()
  132. </script>
  133. </body>
  134. </html>