Home > Web Front-end > JS Tutorial > A brief introduction and examples of variable scope, value transfer, and address transfer in JavaScript Basics_javascript skills

A brief introduction and examples of variable scope, value transfer, and address transfer in JavaScript Basics_javascript skills

WBOY
Release: 2016-05-16 17:31:01
Original
1112 people have browsed it

javascript:变量的声明
以下是几种声明变量的方式

复制代码 代码如下:

 var value;
 var value,value1,value2;//同时声明多个变量,但是这些变量的值都是undefined
 var i = 0,j = 0,k=100;//变量声明,初始化一体。
 //如果大家尝试读一个不存在的变量(值)会报错!但是尝试给一个未使用Var声明的变量赋值,javascript
 //会隐式的声明改变量,而且声明了的变量还是全局的。细节:所以大家创建变量都尽量使用Var
 //变量的作用域(这个问题也容易出,大家要搞明白)

javascript:变量的作用域
 这些都是细节,和我一样初学的一定要注意避免!
复制代码 代码如下:

var golbal = "golbal"; //全局变量
 var local ="local";
function area()
 {
//局部变量的优先级比全局变量的高
var local = "arealocal"
//当函数体内声明的变量名和全局变量名相同时,javascript 会隐藏全局变量
var golbal ="areagolbal";

document.write("local is :"+local + "and golbal is :" + golbal +"
");
 }

area();
//输出:local is :arealocaland golbal is :areagolbal

在嵌套的函数里面定义局部变量,效果会怎么样呢?看下面:
复制代码 代码如下:

var hope = "moremoney";
function createmore()
{
var hope = "have more money";//局部
function createmoreto()//嵌套函数
{
var hope = "have more money to much";//局部
document.write("Createmoreto hope is :"+hope +"
");
  //输出:Createmoreto hope is :have more money to much
}
 createmoreto();//调用
 document.write("Createmore hope is :" +hope +"
");
//输出:Createmore hope is :have more money
}
 createmore(); //调用

javascript:传值和传址
这里也是比较重要的概念!不要漏了。

  传值 传址
复制 实际复制的值,存在不同的、独立的拷贝。 复制的只是对数字的引用。如果通过这个新的引用修改了数值,这个改变对最初的引用来说也是可见的。
传递 传递给函数的是值的独立拷贝对它的改变在函数外没有影响 传递给函数的是对数值的引用,如果函数通过传递给它的引用修改了数值,这个改变也是可见的。
比较 比较这两个对立的值,通常逐字节的比较,以判断是否相等 比较的是两个引用,以判断它们引用的是否是同一个数值。

javascript: basic types and reference types

The basic rules of JavaScript are: basic types are operated by passing by value, and reference types are operated by passing by address. (For what is a value type, or what is a reference, please see my previous article)
Pass by value

Copy code The code is as follows:

var value = 1;
var copyvalue = value; //Assign value to another variable
function addTotal(total,arg)
{
total = arg; //total = total arg has the same effect as
}
//Call the function and pass two parameters (you may think that this function changes the value of the global variable, but it does not. The function also uses opposite copy)
addTotal(value,copyvalue);
if(value == 1) copyvalue = 2;
document.write("total t" value "and copyvalue tt" copyvalue "
");
//Final output: total 1and copyvalue 2

Pass by address
Copy code The code is as follows:

var array = new Array("Javascccp");
var objarray = array;
function modifyArray(arr)
{
arr[0] = "JAVASCRIPT";
}
//Before calling the function
document.write(array[0] "
//Output Javascccp;
//After calling the function
modifyArray(array);
document.write(array[0] "
");
//Output uppercase JAVASCRIPT
//The same effect will be achieved by modifying objarray
objarray[0] = "Frank";
document.write(array[0] "
");
//Output Frank;

Summary: I hope everyone will not miss the above content, it is still very helpful for learning the following knowledge!
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template