DEV Community

Cover image for JS Test #5: Can you use an arrow function as a getter?
Coderslang: Become a Software Engineer
Coderslang: Become a Software Engineer

Posted on • Edited on • Originally published at learn.coderslang.com

10 1

JS Test #5: Can you use an arrow function as a getter?

code-snippet
Are there any issues with the getId function? What will be logged to the screen?

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

So, getId is an arrow function, thus it doesn’t have this of its own.

It’s not bound to this of the obj object and when we try to get this.id it will be evaluated to undefined and not 1.


ANSWER: undefined will be logged to the console.

Top comments (1)

Collapse
 
saurabh_tiwari_e2aaaf6ec7 profile image
Saurabh Tiwari

I have not understand this can you explain

11 Tips That Make You a Better Typescript Programmer

typescript

1 Think in {Set}

Type is an everyday concept to programmers, but it’s surprisingly difficult to define it succinctly. I find it helpful to use Set as a conceptual model instead.

#2 Understand declared type and narrowed type

One extremely powerful typescript feature is automatic type narrowing based on control flow. This means a variable has two types associated with it at any specific point of code location: a declaration type and a narrowed type.

#3 Use discriminated union instead of optional fields

...

Read the whole post now!

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay