Unrecognised javascript syntax

Viewed 68

I have come across a particular javascript code while looking into RxJs documentation.

import { of } from 'rxjs';
import { map } from 'rxjs/operators';

map(x => x * x)(of(1, 2, 3)).subscribe((v) => console.log(`value: ${v}`));

I understand what it does but I have not come across this syntax before as far as I remember. Which syntax is this?

EDIT:

I was confused about the part map(x => x * x)(of(1, 2, 3)). @Olian04 nicely explains that map() is an higher order function. map() returns an function which is passed the parameter of(1, 2, 3)

1 Answers

Since you didn't specify exactly which part of the snipped was confusing you, I'll do a quick breakdown of the code excluding the imports:

  • x => x*x is an arrow function. Its the same as function(x){return x*x;}
  • map is a higher order function that takes a function as its argument map(f). It also returns a function fMap = map(f)
  • fMap is a function that takes an observable as an argument, and returns an observable where each value has had the function f applied on it fObs = fMap(obs)
  • of is a function that takes any number of arguments, and returns an observable that will emit those arguments as events to subscribers obs = of(1, 2, 3)
  • fObs.subscribe is a higher order function that takes a function as its argument and registers it as an event subscriber
  • `value: ${v}` is a string literal template which is the same as doing 'value: ' + String(v)
Related