愿天天向上,愿学以致用,愿掌握生活

javascript (基本类型与引用类型)

上一篇 / 下一篇  2007-10-29 17:05:42 / 天气: 阴 / 心情: 平静 / 个人分类:javascript

var a = 3.14;  //Declare and initialize avariable
var b = a;     //Copy the variable's value to a new variable
a = 4;         //Modify the value of the original variable
alert(b);      //Displays 3.14,the copy has not changed

这段代码没有什么惊人这处,但对这段代码做轻微的发动,使用数组(一个引用类型)代替数值,情况会发生变化:

var a = [1,2,3];  //Initialize a variable to refer to an array
var b = a;        //Copy that reference into a new variable
a[0] = 99;        //Modify the array using the original reference
alert (b);        //Display the changed array [99,2,3] using the new reference

在这个语句中,赋给b的只是对数组值的一个引用,而不是数组本身,数组已经在语句中被赋值了.执行过第二行代码之后,我们仍旧只有一个数组对象,只不过我们有了两个对它的引用.


TAG: javascript

 

评分:0

我来说两句

日历

« 2024-05-02  
   1234
567891011
12131415161718
19202122232425
262728293031 

数据统计

  • 访问量: 21386
  • 日志数: 30
  • 建立时间: 2007-10-02
  • 更新时间: 2007-11-14

RSS订阅

Open Toolbar