fengchuanyu 3 月之前
父节点
当前提交
cb6ef2cac4
共有 1 个文件被更改,包括 40 次插入0 次删除
  1. 40 0
      6_ES6/练习题5_手动实现bind.html

+ 40 - 0
6_ES6/练习题5_手动实现bind.html

@@ -0,0 +1,40 @@
+<!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>
+        // 第一步向Function.prototype上添加一个bind2方法
+        Function.prototype.bind2 = function (obj) {
+            // 返回一个函数
+            let that = this;
+            return function () {
+                that.call(obj);
+            }
+        }
+
+
+        function foo(x,y) {
+            console.log(this.a,x,y)//hello
+        }
+        var obj = {
+            a: "hello"
+        }
+
+
+        var foo2 = foo.bind2(obj,1,2);
+        foo2()
+
+
+
+
+
+    </script>
+</body>
+
+</html>