File tree Expand file tree Collapse file tree 3 files changed +46
-0
lines changed
Expand file tree Collapse file tree 3 files changed +46
-0
lines changed File renamed without changes.
Original file line number Diff line number Diff line change 1+ <!DOCTYPE html>
2+ < head >
3+ < meta charset ="UTF-8 ">
4+ < meta name ="viewport " content ="width=device-width, initial-scale=1.0 ">
5+ < meta http-equiv ="X-UA-Compatible " content ="ie=edge ">
6+ < title > Document</ title >
7+ </ head >
8+ < body >
9+
10+ </ body >
11+ < script >
12+ // 原生js实现bind方法
13+ Function . prototype . bindX = function ( ) {
14+ // 保存原函数
15+ let self = this
16+ // 保存this、剩余参数
17+ // 方式一
18+ // let [thisArg, ...args] = arguments
19+ // 方式二
20+ let thisArg = arguments [ 0 ]
21+ let args = Array . prototype . slice . call ( arguments , 1 )
22+
23+ return function ( ) {
24+ // 方式一
25+ // self.apply(thisArg, [...args, ...arguments])
26+ // 方式二
27+ self . apply ( thisArg , args . concat ( Array . prototype . slice . call ( arguments ) ) )
28+ }
29+ }
30+
31+ function a ( m , n , o ) {
32+ console . log ( this . name + ' ' + m + ' ' + n + ' ' + o ) ;
33+ }
34+ var b = {
35+ name : 'xing'
36+ }
37+ a . bind ( b , 7 , 8 ) ( 9 )
38+ a . bindX ( b , 7 , 8 ) ( 9 ) // xing 7 8 9
39+
40+ // 扩展
41+ // 箭头函数不绑定Arguments 对象
42+ // 箭头函数只能用于非方法函数
43+ // 箭头函数不能作为构造函数
44+ // 箭头函数没有property属性
45+ </ script >
46+ </ html>
File renamed without changes.
You can’t perform that action at this time.
0 commit comments