Skip to content

Instantly share code, notes, and snippets.

@SergeyLipko
Last active April 23, 2017 08:58
Show Gist options
  • Save SergeyLipko/2c5fd182c30eedcab6d0df52a6848e35 to your computer and use it in GitHub Desktop.
Save SergeyLipko/2c5fd182c30eedcab6d0df52a6848e35 to your computer and use it in GitHub Desktop.
const reslut = someFunc(); // запись результат выполнения someFunc в переменную result
const funcLink = somfFunc; // запись ссылки на функцию в переменную funcLink
// * * * * * Default example * * * * *
this.age = 20;
const module = {
age: 12,
showAge: function() {
console.log(this.age);
}
};
module.showAge(); // 12
const useMethod = module.showAge;
// Эквивалентно
// const useMethod = function() {
// console.log(this.age);
// }
// и this теперь указывает на другой age
useMethod(); // 20
const boundUseMethod = module.showAge.bind(module);
boundUseMethod(); // 12
// * * * * * Передача параметров помимо контекста * * * * *
const obj = {
name: 'Martin',
doSomething(...a) {
let summed = a.reduce((a, b) => a + b)
console.log(this.name, summed)
}
}
const pol = {
name: 'Pols'
}
// bind первым аргументом принимает контекст, а затем аргументы
// для целевой функции
const res = obj.doSomething.bind(pol, 2, 4, 89); // "Pols", 95
// * * * * * Частичное применение * * * * *
function someFunc() {
return [...arguments];
// старая альтернатива - превращение "массивоподобного" arguments
// в настоящий массив с помощью Array.prototype.slice
// return Array.prototype.slice(arguments);
}
// частичное применение
let apply23 = someFunc.bind(null, 23); // [23]
let applyAll = apply23(1, 2, 3, 4); // [23, 1, 2, 3, 4]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment