|
@@ -56,16 +56,88 @@
|
|
|
// };
|
|
// };
|
|
|
|
|
|
|
|
//5、请写出下列代码运行结果
|
|
//5、请写出下列代码运行结果
|
|
|
- var name = "window"
|
|
|
|
|
|
|
+ // 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 = {
|
|
var obj = {
|
|
|
- name: "obj"
|
|
|
|
|
|
|
+ a: "hello"
|
|
|
}
|
|
}
|
|
|
- setInterval(function () {
|
|
|
|
|
- console.log(this.name)
|
|
|
|
|
- }, 300)
|
|
|
|
|
- setInterval(function () {
|
|
|
|
|
- console.log(this.name)
|
|
|
|
|
- }.call(obj), 300)
|
|
|
|
|
|
|
+
|
|
|
|
|
+ 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>
|
|
</script>
|
|
|
</body>
|
|
</body>
|
|
|
|
|
|