Created
February 5, 2014 19:47
-
-
Save LESTADru/8831591 to your computer and use it in GitHub Desktop.
вывод вложенных объектов
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="UTF-8"> | |
<title></title> | |
</head> | |
<body> | |
<script> | |
"use strict" | |
var list = { | |
value: 1, | |
next: { | |
value: 2, | |
next: { | |
value: 3, | |
next: { | |
value: 4, | |
next: null | |
} | |
} | |
} | |
}; | |
//вывод вложенных объектов в обратном порядке с помощью массива. | |
function printReverseList2(list){ | |
var tmp = list; | |
var arr = []; | |
while(tmp){ | |
arr.push(tmp.value); | |
tmp = tmp.next; | |
} | |
console.log(arr); | |
for(var i = arr.length - 1; i>=0; i--){ | |
console.log(i); | |
alert(arr[i]); | |
} | |
} | |
printReverseList2(list); | |
//вывод вложеннных объектов. | |
function printList(list){ | |
var tmp = list; | |
while(tmp){ | |
alert(tmp.value); | |
tmp = tmp.next; | |
} | |
} | |
printList(list); | |
//вывод вложенных объектов в обратном порядке с помощью рекурсии. | |
function printReverseList(list){ | |
if(list.next){ | |
printReverseList(list.next); | |
} | |
alert(list.value); | |
} | |
printReverseList(list); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment