JavaScript(2) 引用类型

Value Type and Reference Type

Before talking about their defines and difference ,simple example is showed as following.

var name="Lily";
var nameCopy=name;
nameCopy="LilyCopy";
alert(name);//Lily

var girl={name:"Lily"};
var girlCopy = girl;
girlCopy.name="LilyCopy";
alert(girl.name);//Lily copy

Value type is assigned with a value,when you defined a variable and initialized with a value,then the value is stored in this variable directly.At this moment,if another variable is valued by this variable,the value is cloned and applied to the new variable.From now on ,they have no relations with each other.

Reference types are different,they are designed to describe complex objects.So generally,they means bigger size,if they can be cloned without limited,memory will say "It's terrible!"So when you defined a reference variable ,initialized it with a value,a reference is stored in the variable instead of the value.When you value another reference variable with this variable, a reference is copied to be stored in the new variable.And the value is not copied,there is only one value now in the context.

So when you changed a reference value, variables associated with it are all changed.The following illustration show the relationship:


It's easy to understand the definition,generally speaking,every definition in the world is easy to read,the importance and difficulty is why they are defined like this,why they exist?We are normal people,we cannot talk the world under the gold sign,but as an ITer,we can talk the world of computer science.

So why is reference type designed?

All of the computer world is inherited!At the beginning,one task of computer has its own resource,it's protected and another one can not take use of it.As time going ,different tasks may use the same kind of resource is found,so there are lot of cloned resource in system,they are absolutely same with each other.So thread is created,task is sliced to threads.In human history,this kind of phenomenon is repeated again and again,cycle-usable components are created constantly.

Now,can you understand the essential of  reference-type,it's totally an old concept.




Reference type in javascript

In javascript,reference type is some like class,but it can not be inherited and lacks of other features of class.The reference type contains Object,Array,RegExp,Function

Object

There are two ways to create your own object,showed in the following code.

//use object literal notation to create an object
var studentA ={
	name:"Lily",
	age:18,
	//this means the now-in context:studentA
	introduce:function() {alert("I am "+this.name+","+this.age+".Nice to meet you!");}
}
//use object constructor to create an object
var studentB=new Object();
studentB.name="Lucy";
studentB.age=13;
studentB.introduce= function (){alert("I am "+this.name+","+this.age+".Nice to meet you!");};

studentA.introduce();
studentB.introduce();

The first method is recommended .

Array

Array in javascript is very different from other program language,different types can be stored  in one array,and  it can act like stack or queue.

Different types in same array is showed following,

var arr=[1,"string",studentA];
//It will cause an error if the next line is usable,you should make sure that the element has the function or property
//arr[0].introduce();
arr[2].introduce();
Use array as stack

var arr= new Array();
arr.push(1);
arr.push("string");
var count = arr.push(studentB);//push method returns the length after element pushed.
alert(count);
var ele= arr.pop();//pop method deletes the element of arr in the last position and returns it.
ele.introduce();//make sure the ele has method <span style="font-family: Arial, Helvetica, sans-serif;">introduce .</span>
alert(arr.length);
Use array as queue

var arr= new Array();
arr.unshift(studentA);//unshift method insert an element to the first position of an array and returns the length after element inserted.
arr.unshift("string");
alert(arr.unshift(1));//alert "3",now the elements in arr are as this:[1,"string",studentA].
arr[2].introduce();
var ele=arr.shift();//shift method move the elemtnt in the first position of array out and returns it.
alert(ele);// 1
alert(arr.length);// 2

RegExp

Yeah!Another complex one.I write it here.

Function

Actually function is object.

There are two common ways to create function:

//this function doesnot have a name,but a variable referenced to it
var funa = function(){alert("This is a function a.");}
//
function funb()
{
	alert("This is a function b.")
}

Actually function names are just references to function,the funb above is the same,it is a pointer to the function.That means a function may have different names(references,pointers):

function sum(a,b)
{
	return a+b;
}

var sumx=sum;
sum=null;//sum or sumx is just a pointer to function 
alert(sumx(1,2));//3







  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值