如何在 JavaScript 函数中定义全局变量?

javascriptobject oriented programmingfront end technology更新于 2024/6/25 23:40:17

要声明全局变量,请在全局范围内使用 var −

<script>
   var myGlobalVariable;
   function display() {
      //
   }
</script>

对于 JavaScript 函数,将属性分配给窗口 −

<script>
   function display() {
      window. myGlobalVariable = …;
   }
</script>

相关文章


有用资源