0

What is the of() function in TypeScript/Angular?

For example, https://angular.io/tutorial/toh-pt5 has a reference to: return of(HEROES);

I found the reference to the Observable of() function:

In the sample TOH code url I provided at the first url, the of() function is used independently as opposed to being used with explicit dot notation off an Observable object.

Does the Angular component at the first url I provided maintain some type of implicit component-scoped Observable reference which eliminates the need to call the of() function explicitly via dot notation off of the Observable object? The Observable documentation states:

Observable.of(...items)—Returns an Observable instance that synchronously delivers the values provided as arguments.

TOH is generally the baseline reference model to use for ng development. Is Observable.of() generally considered the default way to return data in ng6? When was the of() function introduced. I don't remember seeing it before ng6?

Angus
  • 3,680
  • 1
  • 12
  • 27
user10307666
  • 151
  • 1
  • 4
  • 14
  • The `of` function does indeed belong to Observable, but as you mention was previously accessed via `Observable.of`. Since rxjs6 (which is used by Angular 6), we must now import `of` as a standalone function (`import { of } from 'rxjs'`). I wouldn't say it's the default way of returning data, but it's a simple way of wrapping your data in an Observable, should you wish to do so – user184994 Sep 25 '18 at 02:37
  • thanks! I wasn't aware that individual functions could be imported like that. is the of() function essentially architected as a type of class in rxjs? or is there some type of special mapping object? are there any other types of objects that can be imported asides from classes and functions? – user10307666 Sep 25 '18 at 06:20
  • 1
    Anything that has been exported can be imported, for example if you do `export const Test = 'Hello'`, you can also then import `Test` in another file. – user184994 Sep 25 '18 at 06:30

0 Answers0