-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path04-bind.html
More file actions
57 lines (43 loc) · 1.25 KB
/
04-bind.html
File metadata and controls
57 lines (43 loc) · 1.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
<script>
Function.prototype.myBind
? Function.prototype.myBind
: Function.prototype.myBind = function(thisObj){
if(typeof this !== "function"){ return;}
var fn = this;
var args = Array.prototype.slice.call(arguments,1);
var funExcessive = function(){};
var funBuild = function(){
//处理new判断
var _this = this instanceof fn ? this : thisObj
// return fn.call(_this, args.concat.apply(args, arguments)) =>自行理解函数将函数参数打散
//以上方法貌似有问题???
// return fn.apply(_this, args.concat.apply(args, arguments))
return fn.apply(_this, args.concat(Array.prototype.slice.call(arguments)))
};
funExcessive.prototype = fn.prototype;
funBuild.prototype = new funExcessive();
// funBuild.prototype = Object.create(fn.prototype);
return funBuild;
};
//简易版 => var self = this;
(function(){
var obj = {
hitch: function(scope, callBack){
return function(){
callBack.apply(scope, arguments || []);
}
}
};
window.sname = "wq";
var o = {
sname:"wangqi",
get:function(){
var self = this;
setTimeout(obj.hitch(self, function(){
console.log(this, "o.get");
}))
}
};
o.get();
})();
</script>