|
@@ -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>
|