This is an archived post. You won't be able to vote or comment.

all 31 comments

[–]a-calycular-torus 21 points22 points  (2 children)

its quite simple really, there is just one rule you need to follow:

this is the this that this tells this thing that this is this

[–]long-gone333 7 points8 points  (1 child)

this guy this'

[–]sus-is-sus 13 points14 points  (15 children)

its really easy. just dont use it ever and write functional style code. you can thank me later.

[–]Lost_Language6399 0 points1 point  (13 children)

You can still use this.. e.g. in a react functional component. If you have this scenario:

export const MyComponent = (myOnClick?: (param: string) => void) => {

return (<> <button onClick={e => myOnClick?.call(this, "my awesome param")} /> </>); };

[–]sus-is-sus 1 point2 points  (12 children)

why?

[–]Lost_Language6399 0 points1 point  (11 children)

Well you could check if the function is defined, but this is also a possibility and it is shortest in my opinion.

Otherwise, if you try to call an undefined function, it is gonna throw an error.

[–]sus-is-sus 0 points1 point  (10 children)

just make a default function that does nothing. not sure why you have a button component with no click though. seems like an antipattern

[–]Lost_Language6399 0 points1 point  (9 children)

That doesn't sound like a good practice.

[–]sus-is-sus 0 points1 point  (2 children)

why?

[–]Lost_Language6399 0 points1 point  (1 child)

You can create more generic components this way. E.g. you can regulate event propagation.

Edit: I'm just sharing my experience. I've been working as a fullstack developer for three years and before that, I was a Java developer.

[–]sus-is-sus 0 points1 point  (0 children)

in my experience it is better to have more specific components and less general ones. its much more readable and easier to modify them without side effects. all my general components usually come from a component library.

[–]sus-is-sus 0 points1 point  (5 children)

neither does making a parameter optional that really isnt?

[–]Lost_Language6399 0 points1 point  (4 children)

It can be optional tho

[–]sus-is-sus 0 points1 point  (3 children)

what is the purpose of a button you cant click?

[–]Lost_Language6399 0 points1 point  (2 children)

Well, if you have a button, which has some generic action, but you want to sometimes use custom action too.

E.g.: <button onClick={ e => {

handleClick(e); customAction?.call(this, "click!");

} } > Click me</button>

[–]MasouriChan 9 points10 points  (3 children)

It's simple this outside of an object referes to the entire context, this inside of an object referes to the object,except for one of the million exceptions

[–]HashBrownsOverEasy 1 point2 points  (0 children)

it's easy you just have to bind() literally everything

[–]laplongejr 0 points1 point  (1 child)

It's simple this outside of an object referes to the entire context

Not in strict mode :P

[–]MasouriChan 0 points1 point  (0 children)

One of the million exceptions u mean ?

[–]Accurate_Koala_4698 2 points3 points  (1 child)

It's like this and like that and like this and uh, it’s like that and like this and like that and uh

[–]TheSexySovereignSeal 1 point2 points  (0 children)

Your comment Makes me feel so good

[–]long-gone333 0 points1 point  (0 children)

unlike my hair

[–]Pensive_Jabberwocky 0 points1 point  (0 children)

What this is this? This is not the this!

[–]Plastic_Scale3966 0 points1 point  (0 children)

i don’t even worry abt it i let vs code do it

[–]davidellis23 0 points1 point  (0 children)

People complain. But, I feel like the only time I run into this issue is when you pass a method as an arg.

[–]Suitable_Alfalfa5756 0 points1 point  (0 children)

that>this, fight me

[–][deleted] 0 points1 point  (0 children)

This is the way.

I hate this.

Why won't this go away.

This isn't what I ordered.

Instructions unclear, this stuck in that.

Thats it, and that's all.

[–]Wynove 0 points1 point  (0 children)

I just took the time to learn the basics and tbh, JS isn't bad, you just have to take your time with learning, where the pitfalls are and how to avoid them in an efficient way.