[译]Javascript中的本地以及全局变量
本文翻译youtube上的up主kudvenkat的javascript tutorial播放单
源地址在此:
https://www.youtube.com/watch?v=PMsVM7rjupU&list=PL6n9fhu94yhUA99nOsJkKXBqokT3MBK0b
Javascript中有两种变量
1.本地变量
2.全局变量
Javascript本地变量:本地变量是在函数里边被申明的.这些变量的作用域在本地,这就是说这些变量仅仅在包含其的函数内部可用.本地变量在函数启动时被制造,然后在函数运行结束后立马被删除.
function helloWorld()
{
var greeting = "Hello";
// The variable greeting is available in the function
greeting = greeting + " JavaScript";
alert(greeting);
} helloWorld(); // The variable greeting is not available outside the function
// Error : 'greeting' is undefined
alert(greeting);
Javascript全局变量:全局变量就是在函数外被声明的变量.全局变量的作用域为全局,这也就是说,所有的在页面上的脚本和函数都可以获取这些变量.全局变量在其被声明后则诞生,而当页面被关闭时才会消亡.
// Global variable
var greeting = "Hello"; function helloWorld()
{
// The variable greeting is available in the function
greeting = greeting + " JavaScript";
alert(greeting);
} helloWorld();
如果你给一个没有被声明的变量赋值的话,那么它就会自动成为一个全局变量,甚至当其在函数内部被呈现也是如此.
function helloWorld()
{
// The variable greeting is not declared but a value is assigned.
// So it will automatically become a global variable
greeting = "Hello JavaScript";
} helloWorld(); // Variable greeting is available outside the function
alert(greeting);
本地变量可以和全局变量有一样的名字.改变一个变量的值并不会对另外一个有影响.如果变量值在函数内部被修改,则其本地变量版本的变量值会被改变.如果变量在函数外被修改的话,则其全局变量版本的变量值会被改变.
var greeting = "This is from global Variable"; function helloWorld()
{
var greeting = "This is from local variable";
document.write(greeting + "[br/]");
} // This line will modify the global greeting variable
greeting += "!!!"; helloWorld(); document.write(greeting);
Output : This is from local variable This is from global Variable!!!
有的时候,本地和全局变量拥有相同的名字,在变量上升(hositing)的影响下,会出现无法预期的行为
var greeting = "This is from global Variable";
helloWorld(); function helloWorld()
{
document.write(greeting);
var greeting = "Hello from local variable"
}
Output : undefined
由于变量上升(hoisting)的存在,以上的程序其实本质上如下:
var greeting = "This is from global Variable";
helloWorld(); function helloWorld()
{
var greeting;
document.write(greeting);
greeting = "Hello from local variable"
}
{}是不会在Javascript中制造作用域的:在以下的例子中,otherNumber是一个在{}内定义的全局变量.在很多其他语言,比如C#或者Java,{}是会制造作用域的,但是Javascript则不然
var number = 100; if (number ] 10)
{
var otherNumber = number;
} document.write(otherNumber);
Output : 100
[译]Javascript中的本地以及全局变量的更多相关文章
- Javascript中的局部变量、全局变量的详解与var、let的使用区别
前言 Javascript中的变量定义方式有以下三种方式:1.直接定义变量,var与let均不写: a = 10; 2.使用var关键字定义变量 var a = 10; 3.使用let关键字定义变量 ...
- [译]Javascript中闭包的各种例子
本文翻译youtube上的up主kudvenkat的javascript tutorial播放单 源地址在此: https://www.youtube.com/watch?v=PMsVM7rjupU& ...
- [译]Javascript中的闭包(closures)
本文翻译youtube上的up主kudvenkat的javascript tutorial播放单 源地址在此: https://www.youtube.com/watch?v=PMsVM7rjupU& ...
- [译]JavaScript中,{}+{}等于多少?
最近,Gary Bernhardt在一个简短的演讲视频“Wat”中指出了一个有趣的JavaScript怪癖:在把对象和数组混合相加时,会得到一些你意想不到的结果.本篇文章会依次讲解这些计算结果是如何得 ...
- [译]Javascript中的错误信息处理(Error handling)
本文翻译youtube上的up主kudvenkat的javascript tutorial播放单 源地址在此: https://www.youtube.com/watch?v=PMsVM7rjupU& ...
- [译]Javascript中的数列
本文翻译youtube上的up主kudvenkat的javascript tutorial播放单 源地址在此: https://www.youtube.com/watch?v=PMsVM7rjupU& ...
- [译]Javascript中的for循环
本文翻译youtube上的up主kudvenkat的javascript tutorial播放单 源地址在此: https://www.youtube.com/watch?v=PMsVM7rjupU& ...
- [译]Javascript中的do-while循环
本文翻译youtube上的up主kudvenkat的javascript tutorial播放单 源地址在此: https://www.youtube.com/watch?v=PMsVM7rjupU& ...
- [译]Javascript中的循环
本文翻译youtube上的up主kudvenkat的javascript tutorial播放单 源地址在此: https://www.youtube.com/watch?v=PMsVM7rjupU& ...
随机推荐
- 「长乐集训 2017 Day1」区间 线段树
题目 对于两个区间\((a,b),(c,d)\),若\(c < a < d\)或\(c < b < d\)则可以从\((a,b)\)走到\((c,d)\)去,现在有以下两种操作 ...
- HDU4825(字典树+贪心)
Xor Sum Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 132768/132768 K (Java/Others)Total S ...
- Numpy:dot()函数
转于:https://www.cnblogs.com/luhuan/p/7925790.html博主:忧郁的白衬衫 一.dot()的使用 1)格式:np.dot(array1, array2) == ...
- SQL 用;with 由所有的子节点查询到树结构中所有父节点
1.所有的子节点查询到树结构中所有父节点 RETURNS @Tree Table(PID )) as begin --DECLARE @ID VARCHAR() --SET @ID = ' ;with ...
- web.xml中classpath表示什么样的路径
首先 classpath是指 WEB-INF文件夹下的classes目录 解释classes含义: 1.存放各种资源配置文件 eg.init.properties log4j.properties ...
- 转:InnoDB Log Block Structure(InnoDB日志Block结构详解)
文章转载自等博 InnoDB Log Block Structure(InnoDB日志Block结构详解)
- Java学习之SpringMVC零配置实践
概述:本实践主要是对SpringMVC的主要功能做了一个大概的体验,将原来的SpringMVC的大量配置改成用SpringBoot进行集成,做到了零XML配置,本次实践分为两个部分,一部分为基本功能实 ...
- 管理react路由的history对象的插件history的使用介绍
本文介绍如何使用history插件管理浏览记录 history插件的使用 history这个插件可以方便管理你的浏览记录 cnpm install history --save import crea ...
- PDM生成数据库-0设置表名和字段名中不带双引号
如果PDM直接导出脚本的话,所有的表和字段都会被加上双引号,非常不方便,去除双引号的办法: Database->Edit Current DBMS在弹出窗体中第一项General中找到 Scri ...
- Qt opencv开发环境
在.pro文件中添加 INCLUDEPATH += C:\opencv\build\include\ #头文件路径 C:\opencv\build\include\opencv\ C:\opencv\ ...