Цитата:
const fn = (a,b)=>a+b; const a = [1,2,3]; const b = [4,5,6]; Array.prototype.sum = function(...args){ return this.reduce(fn) + args[0].reduce(fn)}; console.log(a.sum(b))//21 |
jaroslav.tavgen,
а можно без использования знака +(везде) а fn применить только в строке 6 :) |
jaroslav.tavgen,
хочется сделать более полноценный вариант вашего кода из первого сообщения не слишком изменяя его. |
Цитата:
|
jaroslav.tavgen,
const fn = (a,b)=>a+b; const a = [1,2,3]; const b = [4,5,6]; Array.prototype.sum = function(...args){ return this.reduce(...args)}; console.log(a.sum(fn, b.sum(fn)))//21 |
const fn = (a, b) => a + b; const a = [1, 2, 3]; const b = [4, 5, 6]; Array.prototype.sum = function (...args) { return this.reduce(...args); }; console.log([a, b].map(v => v.sum(fn)).sum(fn, 100)); тоже поучаствую ) |
SuperZen,
:victory: |
const fn = (a,b)=>a+b; const a = [1,2,3]; const b = [4,5,6]; Array.prototype.sum = function(){ return this.reduce(fn)}; console.log(fn(a.sum(), b.sum())) //21 |
voraa,
:) |
Вроде такого еще не было:
const fn = (a,b)=>a+b; const a = [1,2,3]; const b = [4,5,6]; Array.prototype.sum = function() { return this.reduce(...arguments); }; console.log([...a, ...b].sum(fn)) //21 |
Часовой пояс GMT +3, время: 18:42. |