1

I'm reading some JavaScript for work and have come across a section of code I'm finding interesting. I've done some reading and came across:

Is using 'var' to declare variables optional?

Which illuminates the lack of a var type declaration as putting the var into a scope bubbling mutable global-not-global state.

Example:

var foo = bar  // locally scoped
foo = bar // outer scope, can have global ramifications

My question here is when combining outer scope declarations with anonymous functions, is this acceptable and what would be the recommended way of handling this? Is it "good" convention? Should this be locally scoped? It is inside a class for my purposes, so I see no reason why it would be anything other than locally scoped. Again, JS noob though, so looking for some insights.

Example:

updateThing = (e) => {
    this.state.items = e.target.value;
}

Much appreciated, thanks.

John Von Neumann
  • 617
  • 4
  • 15
  • 2
    IMO - keep everything you can defined as clearly as possible. The code might work, but a new developer, or lets say you in a year looking a this might have trouble quickly understanding, having to search for where in the scope you are referring. You may also hit some nasty scope hoisting, you really want to clearly define your vars. I personally don't think it is a "good" convention, but if anyone disagrees I would be curious as to why. – ajmajmajma Nov 15 '17 at 19:56
  • @ajmajmajma see this is largely my thing, the code does not have comments highlighting why it would be in any scope other than local, which makes me question what happens if I refactor and locally scope it. I'm a fan of writing everything to be as explicit as humanly possible, so there's no second guessing, so I might PR it and scope it locally. Thanks for the comment. – John Von Neumann Nov 15 '17 at 20:03
  • Yes that's a great use case as to why you don't want to write things this way. Sorry you have to deal with that! – ajmajmajma Nov 15 '17 at 20:18

1 Answers1

1

Anonymous functions declared without type declarations

There are no anonymous function declarations (except in export default). All the examples you have are assignments or variable declarations with initialisers.

When combining outer scope declarations with anonymous functions, is this acceptable and what would be the recommended way of handling this? Is it "good" convention?

No, this is a bad practice. Just don't do it. Never assign to variables you have not declared anywhere.

It is inside a class for my purposes

If you mean a class definition body: the assignment syntax there is an experimental shortcut for property intialisations in the constructor. Don't use it especially if you don't know what it means. Just write it explicitly:

class … {
    constructor(…) {
         …
         this.updateThing = (e) => {
             this.state.items = e.target.value;
         };
    }
    …
}

This is not a variable assignment and has nothing to do with declarations at all.

Bergi
  • 630,263
  • 148
  • 957
  • 1,375