-2

Thing is I know programming basics, someone told me this is used to encapsulation inside the source of the program (the code with OOP (object oriented programming)).

But I want to know how to implement this programming style successfully, thank you!

I didn't pay attention to it, but as I told you before I could see this is used to make some reference to a variable/property that is similar to another one declared on another place.

function sample(param, parent){
  let _param = "something(specially an object or method from an object)"
}
Camilo Terevinto
  • 31,141
  • 6
  • 88
  • 120
  • 5
    It's just a stylistic choice. It usually means it's an "internal variable" of some sort and other programmers shouldn't change it or use it. But it might also have different meaning than that in different groups. – VLAZ May 30 '19 at 14:38
  • 4
    The _ prefix is a convention normally reserved for variables that are internal to an object, not local variables within a function. The convention is there to make it obvious that they shouldn't be accessed directly by the outside world. – Steve Todd May 30 '19 at 14:39
  • In C# it's a field, which is a variable declared at the class level rather than inside of a method. Once we're used to it, it's a quick way to look at a variable and know (hopefully) whether it was declared in our method or within the class. That, in turn, tells us something about how we should use it. – Scott Hannen May 30 '19 at 14:40
  • For using Visual Studio, I can type the `_` and see all the member fields in the object. I don't use that style for function-local variables. – Ron Beyer May 30 '19 at 14:40
  • 1
    in ES6, `const` declared variables are capitalized (e.g. `const Foo = 3;`) as their object reference is immutable. Let declared variables are camelCase (e.g. `let fooBar = 3;`). An underscore-led variable signifies a configuration variable that is for internal use in the program (e.g. `const _Foo = 3;`). – Len Joseph May 30 '19 at 14:42
  • @LenJoseph I don't think that's official. It's a style guide somewhere but not really widely accepted. I still see plenty of `const foo` around. – VLAZ May 30 '19 at 14:48
  • @VLAZ Although it's not official it's definitely a best practice. We've seen numerous runtime errors when people try to update a constant because they forgot it wasn't a regular mutable variable. – Len Joseph May 30 '19 at 14:49
  • @LenJoseph there are plenty of "good practices" but that doesn't mean they are widely adopted. So saying that "in ES6" something is one way or another is misleading at best. – VLAZ May 30 '19 at 14:51
  • @CamiloTerevinto neither does `oop` to be honest. – VLAZ May 30 '19 at 14:52
  • 1
    @VLAZ just because the community uses the hammer like a wrench doesn't mean it's not a hammer. If you want to refute my point at the peril of maintainability, go ahead, your ego is more important. – Len Joseph May 30 '19 at 14:53
  • @LenJoseph all I'm saying is that "in ES6` a const is nothing. There is no officially accepted way for how to write it. In fact, capitalising the first letters seems to be an outlier - if people adopt a special naming convention, they usually go with [all caps](https://stackoverflow.com/questions/40291766/naming-convention-for-const-object-keys-in-es6) because that's a more standard way to differentiate constants in other languages - in Java you'd very often see `static final FOO`, for example. Not sure why you think my ego is involved when I'm saying ES6 doesn't have an official style guide. – VLAZ May 30 '19 at 14:58
  • @VLAZ I misread your intent, then. I suppose I am an outlier if that's the case. I have seen mutations in caps, so I guess having constants in caps would make sense. At the very least, IMO there should be some differentiation with a regular mutable variable. But, to each their own at the end of the day. – Len Joseph May 30 '19 at 15:03

1 Answers1

0

It is a mild form of hungarian notation to distinguish the scope of a given variable.

Samuel Vidal
  • 883
  • 5
  • 16