1

What is meaning of this piece of code in typescript?

const storeDocumentId = documentId => this.preselectedDocumentId = documentId;

If storeDocumentId is a function how it is getting argument because it is called without argument. Please see function code below.

public  watchViewerRouteChanges(activatedRoute:  ActivatedRoute):  void {
    this.unwatchViewerRouteChanges();

    const  storeDocumentId  =  documentId  =>  this.preselectedDocumentId  =  documentId;
    const  provideDocumentId  =  ()  =>  activatedRoute.snapshot.firstChild.params[QUERY_PARAMS.documentKey];
    const  provideDocument  =  documentId  =>  documentId  ?  this.documentContentsService.getDocumentMetadata(documentId,  false)  :  Observable.of<ToCEntry>(null);

    this.viewerRouteChangeSubscription  =  this.router.events
        .map(provideDocumentId)
        .distinctUntilChanged()
        .do(storeDocumentId)
        .do(()  =>  this.showDocumentContentsHeader  =  false)
        .switchMap(provideDocument)
        .subscribe(selectDocument);
}
Shailesh Vikram Singh
  • 1,093
  • 10
  • 21
  • Possible duplicate of [When should I use Arrow functions in ECMAScript 6?](https://stackoverflow.com/questions/22939130/when-should-i-use-arrow-functions-in-ecmascript-6) – Makyen Apr 13 '18 at 10:27

2 Answers2

2

The left hand side of the expression creates a variable called storeDocumentId, which will be the function defined on the right hand side.

The function takes a single argument, documentId, and assign it to the preselectedDocumentId member.

It uses an arrow function, which preserves the scope of this - which is useful if the function will be called from other scopes, such as events.

Here is an old-fashioned version that doesn't use an arrow function:

var _this = this;
var storeDocumentId = function (documentId) { 
    return _this.preselectedDocumentId = documentId;
};

Note that the value is returned in this function unnecessarily - I have done this to keep the old-fashioned version identical to the original. When an arrow function has a single expression, it is returned by default.

If you writing it the old way, you probably wouldn't have a return statement.

Fenton
  • 206,497
  • 63
  • 356
  • 369
  • But storeDocumentId function is called below without argument. How it is getting argument? – Shailesh Vikram Singh Apr 13 '18 at 08:23
  • 1
    In this line `.do(storeDocumentId)` the `storeDocumentId` function isn't called - it is passed to the `do` function, which may then call it. Inside the `do` function you should see how it calls `storeDocumentId`. – Fenton Apr 13 '18 at 09:45
1

It declares a constant named storeDocumentId and assigns an arrow function to it. The arrow function has a parameter named documentId and the function body is this.preselectedDocumentId = documentId. Which assigns the parameter to the field preselectedDocumentId of the object (this) where this arrow function is declared

Titian Cernicova-Dragomir
  • 157,784
  • 15
  • 245
  • 242