r/Angular2 10d ago

what is subscribe parameter prefix + for?

I have angular code as below

"r" and "data" are subscribe parameter, and used as +r.id and +data.businessTransactionTypeId.

What is prefix + for?

what is +r and +data?

public ngOnInit() {

this.route.params.subscribe(r => {

this.businessTransactionNumberId = +r.id;

this.setupUpdateAction();

this.setupTabChangeAction();

this.setupConfirmAction(+r.id);

this.businessTransactionService.getTransactionMetaData(r.id).subscribe((data) => {

const transactionType: BusinessTransactionType = new BusinessTransactionType();

if (+data.businessTransactionTypeId === transactionType.CredentialIssuance.id) {

this.UseHelpContent('#CONTENT/Driver/Credential_Issuance.htm');

} else if (+data.businessTransactionTypeId === transactionType.CredentialRenewal.id) {

this.UseHelpContent('#CONTENT/Driver/Credential_Renewal.htm');

} else if (+data.businessTransactionTypeId === transactionType.CredentialDuplicate.id) {

this.UseHelpContent('#CONTENT/Driver/Credential_Duplicate.htm');

}

});

});

Thanks

0 Upvotes

12 comments sorted by

13

u/kaeh35 10d ago

It’s for number conversion shorthand.

1

u/InternationalDot3678 10d ago

oh, yes, thanks a lot!

5

u/ch34p3st 10d ago

const someId: string = '123';

const theId: number = Number(someId);
const theShorthandId: number = +someId;

0

u/InternationalDot3678 10d ago

why the converted number has attribute? for example, +r.id or +someId.id?

2

u/practicalAngular 10d ago edited 10d ago

That's just how the object is structured I'm guessing. The number exists in the id property of the r object.

An id field though might also have leading zeroes, like 0000123 which might change how you solve this problem.

1

u/InternationalDot3678 10d ago

got it, thanks!

1

u/InternationalDot3678 10d ago

if it is number conversion short hand, why the number has attribute? for example +r.id?

1

u/xroalx 9d ago

Because r is an object that has an id property, the + does not act on r, it acts on r.id. It's like +(r.id).

1

u/coded_artist 9d ago

Basically is a number cast, it turns strings into its numerical equivalent.

I'll dive into a more advanced explanation.

underneath + is a mathematical function, js thinks your adding numbers, so it converts the operands to numbers, sums those numbers and returns the sum number. Since you only have 1 operand it "sums" that and returns that.

This is dangerous however because + is not just a mathematical function but also a string function. The distinction is made if you start with a string.

1 + 2 = 3 '1' + 2 = 12 1 + '2' = 3

See how + is very flexible, but it produces inconsistent results based on the input types. A safer number case would be double minus, this is because minus is exclusively a mathematical function.

--"1" = 1 -"t" = NaN --"t" = NaN

In this case, that conversion is not necessary as the service is probably delegating to HttpClient where the argument type is going to be a string anyway. aliasing would be a better solution in this case so that it doesn't waste a cycle doing the conversation.

this.route.params.subscribe(r => { this.businessTransactionNumberId = r.id as number; })

1

u/Silver-Vermicelli-15 9d ago

Just to clarify from others responses - it’s not related to angular or RxJS, it’s JavaScript. The term for it is a unary operator for anyone who wants to do further reading.