en.javascript.info/1-js/6-objects-more/7-bind/1-cross-browser-bind/task.md
2015-01-14 10:23:45 +03:00

22 lines
925 B
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# Кросс-браузерная эмуляция bind
[importance 3]
Если вы вдруг захотите копнуть поглубже -- аналог `bind` для IE8- и старых версий других браузеров будет выглядеть следующим образом:
```js
function bind(func, context /*, args*/) {
var bindArgs = [].slice.call(arguments, 2); // (1)
function wrapper() { // (2)
var args = [].slice.call(arguments);
var unshiftArgs = bindArgs.concat(args); // (3)
return func.apply(context, unshiftArgs); // (4)
}
return wrapper;
}
```
Использование -- вместо `mul.bind(null, 2)` вызывать `bind(mul, null, 2)`.
Не факт, что он вам понадобится, но в качестве упражнение попробуйте разобраться, как это работает.