| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144 |
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>Document</title>
- </head>
- <body>
- <script>
- //1、写出下列输出结果
- // 全局变量才会放到window对象中
- // var x = 10;
- // function test() {
- // var x = 20
- // console.log(this.x)
- // }
- // test()
- //2、写出下列输出结果
- // var name = "window"
- // var obj = {
- // name: "obj",
- // func1: function () {
- // console.log(this.name);//obj
- // (function () {
- // console.log(this.name);//window
- // })()
- // }
- // }
- // obj.func1()
- //3、写出下列结果
- // var name = "the window";
- // var object = {
- // name: "My Object",
- // getName: function () {
- // return this.name;
- // }
- // }
- // console.log(object.getName());//My Object
- // console.log((object.getName)());//My Object
- // console.log((object.getName = object.getName)());//the Window
- // let a = 10;
- // if(a=3){
- // console.log(a);
- // }
- //4、下列代码中当div的点击事件触发时输出的结果是?
- // document.getElementById("div").onclick = function () {
- // console.log(this);//div
- // };
- //5、请写出下列代码运行结果
- // var name = "window"
- // var obj = {
- // name: "obj"
- // }
- // setInterval(function () {
- // console.log(this.name);//window
- // }, 300)
- // setInterval(function () {
- // console.log(this.name);//obj
- // }.call(obj), 300)
- //6、请补全下列代码
- // function foo() {
- // //补全此处代码实现每隔一秒输出 hello world
- // // console.log("hello world");
- // return function(){
- // console.log("hello world");
- // }
- // }
- // window.setInterval(foo(), 1000);
- // 7、补全下列代码实现 1+2+3+4
- // function add(c, d) {
- // return this.a + this.b + c + d;
- // }
- // var obj = {
- // a:1,
- // b:2
- // }
- // add.call(obj,3,4);
- /*
- 在此补全代码 以两种以上方法实现
- */
- //8、写出下列输出结果
- // function f() {
- // return this.a;
- // }
- // var g = f.bind({ a: "azerty" });
- // console.log(g());//azerty
- // // bind 不可以重复绑定 如果重复绑定 会返回第一个绑定的结果
- // var h = g.bind({ a: 'yoo' });
- // console.log(h());//azerty
- // var o = { a: 'loveCoding', f: f, g: g, h: h };
- // console.log(o.f(), o.g(), o.h());//loveCoding azerty azerty
- // //9、补全下列代码
- // var o = { prop: 'loveCoding' };
- // function independent() {
- // return this.prop;
- // }
- // //在此补全代码
- // o.f = independent.bind(o);
- // console.log(o.f()); // loveCoding
- //10、用call 或 apply 实现bind 方法
- function foo(b,c,d) {
- console.log(this.a,b,c,d)
- }
- var obj = {
- a: "hello"
- }
- Function.prototype.bind2 = function(){
- // arguments 是类数组对象 可以获取所有参数
- // console.log(arguments);
- var newObj = arguments[0];
- // 获取所有参数
- var args = Array.from(arguments).slice(1);
- console.log(args);
- var _this = this;
- return function(){
- _this.apply(newObj,args)
- }
- }
- var foo2 = foo.bind2(obj,"world","你好","世界");
- foo2()
- </script>
- </body>
- </html>
|