| 12345678910111213141516171819202122232425262728293031323334353637383940 |
- <!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>
|