Javascript引用指针
2012-11-04 09:40
246 查看
[b]Quiz[/b]
请尝试完成下列完形填空:
这是一个用Javascript写的链表题。由于链表拥有较为复杂的引用操作,正好可以用来考察下对Javascript的引用的理解。附带简单的测试用例:
[b]Javascript引用?[/b]
实际上,在对变量赋予引用类型实例时,变量保存的是该实例的引用:
var temp = new Object();
这种表现非常切合它的名字,引用类型,其实例当然是来引用的。
而当将该变量再付给另一个变量的时候,实际上只是进行了对引用的复制操作:
var temp2 = temp;
所以虽然从定义式来看:temp2 = temp,但他们并没有直接联系,例如修改了temp的引用:
当然,如果我们修改的只是指针指向的实例本身,那么temp2依然等于temp:
[b]IntList是什么东东?[/b]
我们来分析一下下图:
创建两个空的变量,所以右图中L和Q是空的。
创建一个新的IntList其头部为3,尾部为空,将L引用的值赋给Q,所以L和Q都指向这个新的IntList。
Q指向一个新创建的IntList其头部为42,尾部为空,将Q的指针赋给L.tail,这样两个IntList被套嵌起来。
可见IntList是个通过指针达到多重套嵌的数据结构,被称为链表(Linked List)。
[b]IntList合并[/b]
我们只需要将其中一个的尾部指向另一个就行了。这样这两个IntList就连接起来了:
[b]IntList截取[/b]
由于题目要求不能改变原IntList,所以我们只能从原IntList取出数据再重建一个新的数据。
[b]思考题[/b]
1. 函数传参数的时候是怎么传的?例如下面代码的引用过程是怎样的?
[b]扩展阅读[/b]
Javascript的this关键字详解
请尝试完成下列完形填空:
/* 创建一个队列,头为head0,尾为tail0 */ function IntList(head0, tail0){ this.head = head0 || 0; this.tail = tail0 || null; } /* 返回一个IntList包含数组中的所有数 */ IntList.list = function(__args){ var sentinel = new IntList(), len = __args.length, p; p = sentinel; for(var i = 0; i < len; i++){ p.tail = new IntList(__args[i]); p = p.tail; } return sentinel.tail; }; /* 返回该对象的字符串表示 */ IntList.prototype.toString = function(){ var temp = ""; temp += "["; for(var L = this; L !== null; L = L.tail){ temp = temp + " " + L.head; } temp += " ]"; return temp; }; /** 返回一个IntList,包含IntList A和IntList B, * 其中B的元素在A的后面。不能使用new关键字。 */ function dcatenate(A, B){ /* 完成功能 */ } /** 返回一个新的IntList,其长度为len, * 以#start元素为开头(其中#0是第一个元素), * 不能改变L。 */ function sublist(L, start, len){ /* 完成功能 */ }
这是一个用Javascript写的链表题。由于链表拥有较为复杂的引用操作,正好可以用来考察下对Javascript的引用的理解。附带简单的测试用例:
/* 测试dcatenate和sublist函数是否正确 */ function test(){ var A = IntList.list([4,6,7,3,8]), B = IntList.list([3,2,5,9]); dcatenate(A, B); if(A.toString() === "[ 4 6 7 3 8 3 2 5 9 ]"){ alert("dcatenate函数正确。"); }else{ alert("dcatenate函数错误。"); } var L = IntList.list([3,4,5,2,6,8,1,9]), result = sublist(L, 3, 3); if(result.toString() === "[ 2 6 8 ]"){ alert("sublist函数正确。"); }else{ alert("sublist函数正确。"); } }
[b]Javascript引用?[/b]
实际上,在对变量赋予引用类型实例时,变量保存的是该实例的引用:
var temp = new Object();
这种表现非常切合它的名字,引用类型,其实例当然是来引用的。
而当将该变量再付给另一个变量的时候,实际上只是进行了对引用的复制操作:
var temp2 = temp;
所以虽然从定义式来看:temp2 = temp,但他们并没有直接联系,例如修改了temp的引用:
var temp = { name: "temp" }; var temp2 = temp; temp = { name: "not temp2" }; temp === temp2; //false
当然,如果我们修改的只是指针指向的实例本身,那么temp2依然等于temp:
var temp = { name: "temp" }; var temp2 = temp; temp.name = "also temp2"; temp === temp2; //true
[b]IntList是什么东东?[/b]
我们来分析一下下图:
创建两个空的变量,所以右图中L和Q是空的。
创建一个新的IntList其头部为3,尾部为空,将L引用的值赋给Q,所以L和Q都指向这个新的IntList。
Q指向一个新创建的IntList其头部为42,尾部为空,将Q的指针赋给L.tail,这样两个IntList被套嵌起来。
可见IntList是个通过指针达到多重套嵌的数据结构,被称为链表(Linked List)。
[b]IntList合并[/b]
我们只需要将其中一个的尾部指向另一个就行了。这样这两个IntList就连接起来了:
/** 返回一个IntList,包含IntList A和IntList B, * 其中B的元素在A的后面。不能使用new关键字。 */ function dcatenate(A, B){ var p; for(p = A; p != null; p = p.tail){ if(p.tail === null){ p.tail = B; break; } } return A }
[b]IntList截取[/b]
由于题目要求不能改变原IntList,所以我们只能从原IntList取出数据再重建一个新的数据。
/** 返回一个新的IntList,其长度为len, * 以#start元素为开头(其中#0是第一个元素), * 不能改变L。 */ function sublist(L, start, len){ var K, P, J; var i = 0, end = start + len; for(P = L; i <= end; P = P.tail, i++){ if(i < start){ continue; }else if(i === start){ K = new IntList(P.head); J = K; }else if(i > start && i < end){ J.tail = new IntList(P.head); J = J.tail; }else if(i >= end){ break; } } return K; }
[b]思考题[/b]
1. 函数传参数的时候是怎么传的?例如下面代码的引用过程是怎样的?
var obj = { name: "anything" }; function getName(__obj){ return __obj.name; } var name = getName(obj);
[b]扩展阅读[/b]
Javascript的this关键字详解
相关文章推荐
- JavaScript中变量、指针和引用功能与操作示例
- 谈谈JavaScript中的变量、指针和引用
- Javascript引用指针使用介绍
- javascript 中的声明作用域范围以及指针引用的问题。
- javascript引用类型指针
- Javascript引用指针使用介绍
- javascript引用类型指针的工作方式
- 一个关于JavaScript引用数据类型指针的小题
- 不要返回局部对象的引用或指针
- 指针与引用的区别以及引用的三种用法
- 函数返回引用和指针类型
- C++学习笔记(二)引用与指针
- C++引用和指针的区别 —— 笔记
- 引用-基本数据类型,指针
- 浅谈C++中指针和引用的区别
- C++中引用、指针与const之间的爱恨情愁
- JavaScript之引用类型介绍
- C++中的值传递,引用传递,指针传递
- C/C++中指针和引用之相关问题研究
- JavaScript基础学习<四>基本数据和引用类型