JavaScript reference pointer use introduction _javascript tips

Source: Internet
Author: User
Please try to complete the following cloze:
Copy Code code as follows:

/* Create a queue with head head0, tail for tail0 * *
function Intlist (head0, tail0) {
This.head = Head0 | | 0;
This.tail = Tail0 | | Null
}
/* Returns a intlist containing all the numbers in the array * *
Intlist.list = function (__args) {
var Sentinel = new Intlist (),
Len = __args.length,

p = Sentinel;
for (var i = 0; i < len; i++) {
P.tail = new Intlist (__args[i]);
p = p.tail;
}
return sentinel.tail;
};
/* Returns the string representation of the object * *
IntList.prototype.toString = function () {
var temp = "";
temp = "[";
for (var L = this; L!== null; L = L.tail) {
Temp = temp + "" + l.head;
}
temp = "]";
return temp;
};

/** returns a intlist containing intlist A and intlist B,
* Where the elements of B are behind a. You cannot use the New keyword.
*/
function Dcatenate (A, B) {
/* Complete function * *
}

/** returns a new intlist, the length of which is Len,
* Starts with the #start element (where #0 is the first element),
* Can't change L.
*/
function sublist (L, start, Len) {
/* Complete function * *
}

This is a linked list problem written in JavaScript. Because a linked list has a more complex reference operation, it can be used to examine the understanding of JavaScript references. Comes with simple test cases:
Copy Code code as follows:

/* Test dcatenate and sublist functions are correct * *
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 function is correct.) ");
}else{
Alert ("Dcatenate function error.) ");
}
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 function is correct.) ");
}else{
Alert ("sublist function is correct.) ");
}
}

JavaScript reference?

In fact, when assigning a reference type instance to a variable, the variable holds a reference to the instance:

var temp = new Object ();

This performance is well suited to its name, reference type, and its instance is, of course, cited.

When the variable is then paid to another variable, it is actually just a copy of the reference:

var temp2 = temp;

So although in terms of definition: Temp2 = temp, they are not directly related, such as modifying the temp reference:

Copy Code code as follows:

var temp = {
Name: "Temp"
};
var temp2 = temp;
temp = {
Name: "Not Temp2"
};
temp = = Temp2; False

Of course, if we modify only the instance that the pointer points to, then temp2 still equals temp:
Copy Code code as follows:

var temp = {
Name: "Temp"
};
var temp2 = temp;
Temp.name = "also Temp2";
temp = = Temp2; True

What is Intlist?

Let's analyze the following figure:

      Create two empty variables, so l and Q are empty in the right picture. Create a new intlist whose head is 3, the tail is empty, assign the value of L reference to Q, so L and Q all point to the new intlist. Q points to a newly created intlist whose head is 42, the tail is empty, and the pointer to the Q is assigned to L.tail so that the two intlist quilt is embedded.

Visible intlist is a data structure that achieves multiple nesting through pointers, known as a linked list (linked list).

1. Create two empty variables, so l and Q are empty in the right picture.
2. Create a new intlist whose head is 3, the tail is empty, assign the value of L reference to Q, so L and Q all point to the new intlist.
3.Q points to a newly created intlist whose head is 42, the tail is empty, and the pointer to the Q is assigned to L.tail so that the two intlist quilt is embedded.
Visible intlist is a data structure that achieves multiple nesting through pointers, known as a linked list (linked list).

Intlist Merge

We just need to point one of the tails to the other. So the two intlist are connected:

Copy Code code as follows:

/** returns a intlist containing intlist A and intlist B,
* Where the elements of B are behind a. You cannot use the New keyword.
*/
function Dcatenate (A, B) {
var p;
for (p = A; p!= null; p = p.tail) {
if (P.tail = = null) {
P.tail = B;
Break
}
}
Return A
}

Intlist Intercept
Because the problem requirements can not change the original intlist, so we can only remove the data from the original intlist and reconstruct a new data.
Copy Code code as follows:

/** returns a new intlist, the length of which is Len,
* Starts with the #start element (where #0 is the first element),
* Can't change 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;
}

Study Questions
1. How does the function pass the parameter? For example, what is the reference procedure for the following code?
Copy Code code as follows:

var obj = {
Name: "Anything"
};
Function GetName (__obj) {
return __obj.name;
}
var name = GetName (obj);
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.