-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunction.js
68 lines (59 loc) · 2.34 KB
/
function.js
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
58
59
60
61
62
63
64
65
66
67
68
/**
* Object.prototype.bound() function: The opposite side of Function.prototype.bind.
* Copyright (c) 2015 Vladislav Zarakovsky
* MIT license https://github.com/vlazar/object-bound/blob/master/LICENSE
*
* It is very fast! Adds as little overhead as possible: https://jsperf.com/object-bound
* Special thanks to an amazing https://github.com/codemix/fast.js
*/
(function () {
var bind = Function.prototype.bind
/* eslint-disable no-extend-native */
Object.defineProperty(Object.prototype, 'bound', {
configurable: true,
value: boundMethod
})
function boundMethod (name) {
var self = this
var method = self[name]
// inlined version of applyWithContext() from fast.js
switch (arguments.length) {
case 0:
return bind.call(method)
case 1:
return bind.call(method, self)
case 2:
return bind.call(method, self, arguments[1])
case 3:
return bind.call(method, self, arguments[1], arguments[2])
case 4:
return bind.call(method, self, arguments[1], arguments[2], arguments[3])
case 5:
return bind.call(method, self, arguments[1], arguments[2], arguments[3], arguments[4])
case 6:
return bind.call(method, self, arguments[1], arguments[2], arguments[3], arguments[4], arguments[5])
case 7:
return bind.call(method, self, arguments[1], arguments[2], arguments[3], arguments[4], arguments[5], arguments[6])
case 8:
return bind.call(method, self, arguments[1], arguments[2], arguments[3], arguments[4], arguments[5], arguments[6], arguments[7])
default:
var args = fastCloneArguments.apply(null, arguments)
// not inlining this into fastCloneArguments gives better performance in Firefox
args[0] = self
return bind.apply(method, args)
}
}
// Modified version of fastCloneArray() from fast.js for use with arguments.
// Use fastCloneArguments.apply(null, arguments), not fastCloneArguments(arguments)!
// See benchmark http://jsperf.com/fast-js-fastclonearray-with-arguments
// More details https://github.com/petkaantonov/bluebird/wiki/Optimization-killers#what-is-safe-arguments-usage
function fastCloneArguments () {
var len = arguments.length,
args = new Array(len),
i
for (i = 0; i < len; i++) {
args[i] = arguments[i]
}
return args
}
})()