What does it mean _: e _

4

What does _: e _ mean? in the definition of functions as in the example below.

constructor(){
  router.events.subscribe((_:NavigationEnd) => this.currentUrl = _.url);
}

I have many doubts about it.

    
asked by anonymous 04.08.2018 / 16:36

1 answer

8

The underscore ( _ ) is a variable. The dot after it means that it is an object and one of its properties is being accessed

let _ = {
  foo: 'bar',
  baz: v => 'foo bar baz ${v}'
}

console.log(_)
console.log(_.foo)
console.log(_.baz('taz'))

As quoted in the comments by @HebertdeLima, Underscore.js uses this character as a "parent object" containing the functions and values of the library

The underscore also is (it is a convention, not an obligation) to be used for properties and private functions of an object:

function Foo(_bar) {
  this.setBar = (bar) => { _bar = bar  }
  this.getBar = ()    => { return _bar }
}

let baz = new Foo('taz')
console.log(baz._bar)
console.log(baz.getBar())

In TypeScript, the colon (% w / o%) indicates what type this variable should be:

let foo :string //Tipo string

interface Baz {
  let taz :boolean; //Baz é um objeto com uma propriedade taz, do tipo booleana
}

let bar :Baz //Tipo Baz 
    
04.08.2018 / 16:47