javascript variable scope in the IF statement

后端 未结 5 1961
粉色の甜心
粉色の甜心 2020-11-30 23:53

Are variables declared and assigned in an \"if\" statement visible only within that \"if\" block or within the whole function?

Am I doing this right in the followin

5条回答
  •  感动是毒
    2020-12-01 00:34

    are variables declared and assigned in an "if" statement visible only within that "if" block or within the whole function?

    In Javascript, all variables are either

    • global scope
    • local scope (entire function) - Javascript doesn't have a "block scope" where variables are only available with a smaller block of the local scope (function)

    am i doing this right in the following code? (seems to work, but declaring "var structure" multiple times seems akward) any cleaner solutions?

    Yes. A cleaner solution might be to build a base class of structure, and modify what is different in each case.

提交回复
热议问题