It is permissible to use the same name for the class and variable in the example class settingsBlock and the let settingsBlock object. Or the name of the object should not be in camelCase?

 class settingsBlock extends elementByClass { open () { this.classList.toggle('settings_opened'); } } let settingsBlock = new settingsBlock('settings'); document.getElementsByClassName('settings-button')[0].onclick = function () { settingsBlock.open(); }; 

Poke, please, in the documentation. All the examples that have been seen use simple words like "menu, settings, button" to denote a variable, which is inconvenient in large projects.

  • Here is a good agreement on the name of the agreement - Mikl
  • @Mikl did not understand why you suggested BEM? it's about js - while1pass
  • I saw the settings-button and settings_opened in one code and decided that it would be useful for the author to deal with it - Mikl
  • @Mikl and what is wrong with them? - while1pass

2 answers 2

No, this is unacceptable.

When creating a class, or declaring a variable using let or const with a name that already exists, an exception will be thrown:

Identifier '' has already been declared

And the problem is not camel-case .

  • Comments are not intended for extended discussion; conversation moved to chat . - Nicolas Chabanovsky

When you declare a variable, function, or something else, a scope is created. A scope is roughly an object that stores references to objects created within this scope.

So this scope as well as Object stores all values ​​as properties, for which keys a string is used. This means that both the variable and the function will look something like this -

 var propName = 5; function functionName(){}; { propName: propName, functionName: functionNane } 

It is also necessary to remember that the variable name is a link to an object and it follows from this that in the same scope there can not be two identical keys - two identical links.

And separately about naming - in js it is common to use java style to name variables, types, constants and methods

  • var propName
  • let propName
  • const CONST_NAME
  • function functionName () {}
  • class ClassName {}
  • Comments are not intended for extended discussion; conversation moved to chat . - PashaPash