javascript当中局部变量和全局变量

博客介绍了局部变量和全局变量相关知识。在浏览器中,window 相当于 global 且通常可省略;nodejs 里没有 window 但有 global。还通过代码示例展示了有 var 无 var 在 function 内外定义变量时,变量是局部还是全局的不同情况。

2)局部变量和全局变量 
马克-to-win:浏览器里面 window 就是 global,通常可以省。
nodejs 里没有 window,但是有个叫 global 的。

例 3.2.1
<html>
<head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8"/>
</head>
<script>
/* 马克-to-win:有var无var, 在function外是一样的,都是全局的,在function里面时,var是局部的,而无var时是代表全局的*/   
    var testVar = "全量";
    document.writeln("window.testVar is" + window.testVar+testVar);
    var testqVar = "全量q";
/*如不屏蔽下句话,程序直接停在这了,因为出错了,不认识testGlobal,得把下一句和下下句换一下位置,就ok了 */
  //  document.writeln("testGlobal is" + testGlobal);
    testGlobal = "全量global";
    document.writeln("abc is" + abc);
    var abc;
    testGlobalInVar = "全量globalInVar";
    function testSco()
    {
        var lll = "qqq";
        var testVar = "局量"; //此testVar非外面的testVar
        testqVar = "全量qchange"; //此testqVar就是外面的testqVar
        testGlobal = "全量globalchange";
        var testGlobalInVar = "局量global";//此testGlobalInVar非外面的testGlobalInVar
        /*local variable is stronger than global variable.so "testVar" in the following statement means local variable.*/
        document.writeln(testVar);
        document.writeln(testqVar);
        document.writeln("testGlobalInVar is " + testGlobalInVar);
    }
    testSco();
    document.writeln("second test is " + testVar);
    document.writeln("second testqVar is " + testqVar);
    document.writeln("testGlobal is " + testGlobal);
    document.writeln("testGlobalInVar is " + testGlobalInVar);
</script>

更多请见:https://blog.youkuaiyun.com/qq_44594249/article/details/99864142

### 局部变量全局变量的作用域 局部变量是在函数或代码块内部声明的变量,其作用域仅限于该函数或代码块内部。这意味着局部变量只能在定义它的代码块内被访问修改,外部无法直接访问[^4]。例如,在以下代码中,`x` 是一个局部变量: ```python def example_function(): x = 10 print(x) example_function() # print(x) # 这行代码会引发 NameError,因为 x 是局部变量 ``` 全局变量则是在函数外部声明的变量,其作用域覆盖整个程序,可以在多个函数中被访问修改。例如: ```python y = 20 def another_function(): print(y) another_function() print(y) ``` ### 局部变量全局变量的优先级关系 当局部变量全局变量同名时,在函数内部优先使用局部变量。这种规则被称为“局部变量优先”原则。如果在函数内部需要访问全局变量,则必须显式地使用作用域解析操作符(如 `::` 在 C++ 中)或者通过其他语言特定的方式进行区分。 例如,在 C 语言中: ```c #include <stdio.h> int count = 0; // 全局变量 void increment() { int count = 10; // 局部变量 printf("%d\n", count); // 输出局部变量的值 } int main() { increment(); return 0; } ``` 在这个例子中,尽管全局变量 `count` 存在,但在 `increment()` 函数内部,局部变量 `count` 被优先使用[^1]。 在 Python 中,可以通过 `global` 关键字来显式地引用全局变量: ```python count = 0 # 全局变量 def increment(): global count count += 1 increment() print(count) # 输出 1 ``` 如果没有使用 `global` 关键字,Python 会在函数内部创建一个新的局部变量而不是修改全局变量JavaScript 中的情况略有不同,由于变量提升机制的存在,即使在函数内部声明了同名变量,也会导致全局变量被隐藏: ```javascript var val = 10; function test() { console.log(val); // undefined var val = 100; console.log(val); // 100 } test(); ``` 在这个 JavaScript 示例中,虽然第一个 `console.log(val)` 看起来应该输出全局变量 `val` 的值 `10`,但由于变量提升,局部变量 `val` 已经存在但尚未赋值,因此输出为 `undefined` [^5]。 ### 生命周期与存储方式 局部变量的生命周期始于声明位置,结束于代码块执行完毕。它们通常存储在栈内存中,并且每次调用对应的函数或进入代码块时会创建新的副本,互不干扰。全局变量具有全局可见性,在整个程序中都可以访问修改,其生命周期从定义位置开始,直到程序结束[^3]。 ---
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

mark_to_win

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值