admin管理员组文章数量:1426463
The book says:
To run over a list (in listToArray and nth), a for loop specification like this can be used:
for (var node = list; node; node = node.rest) {}
Every iteration of the loop, node points to the current sublist, and the body can read its value property to get the current element. At the end of an iteration, node moves to the next sublist. When that is null, we have reached the end of the list and the loop is finished.
Question 1: Could you explain how the condition of the for loop works? I understand that it is checking whether the node(the current list) is null.. but how does the "node" argument by itself work?
Question 2: why doesnt the following code work?
function listToArray(list){
var result = [];
while(list.value != null){
result.push(list.value);
list = list.rest;
}
return result;
};
console.log(listToArray(list));
The book says:
To run over a list (in listToArray and nth), a for loop specification like this can be used:
for (var node = list; node; node = node.rest) {}
Every iteration of the loop, node points to the current sublist, and the body can read its value property to get the current element. At the end of an iteration, node moves to the next sublist. When that is null, we have reached the end of the list and the loop is finished.
Question 1: Could you explain how the condition of the for loop works? I understand that it is checking whether the node(the current list) is null.. but how does the "node" argument by itself work?
Question 2: why doesnt the following code work?
function listToArray(list){
var result = [];
while(list.value != null){
result.push(list.value);
list = list.rest;
}
return result;
};
console.log(listToArray(list));
Share
Improve this question
edited May 13, 2015 at 20:24
Felix Kling
818k181 gold badges1.1k silver badges1.2k bronze badges
asked May 13, 2015 at 20:22
Jenny Jenny
234 bronze badges
1
- 1 For what it's worth in modern JavaScript you'd make the list iterable rather than rolling your own iteration scheme. – Benjamin Gruenbaum Commented May 13, 2015 at 20:38
4 Answers
Reset to default 5To understand how this works, you need to know two things:
- How java script For loop works.
- What are truthy values.
The syntax
for the for loop statement:
for ([initialization]; [condition]; [final-expression]) statement
[condition] An expression to be evaluated before each loop iteration. If this expression evaluates to true, statement is executed. If the expression evaluates to false, execution skips to the first expression following the for construct.
In Java script, a truthy
value evaluates to true when evaluated in a Boolean context.
All values are truthy unless they are defined as falsy (i.e., except for false, 0, "", null, undefined, and NaN).
So till node
is null
at one point, we may say that node
is truthy
and evaluates to true.
When the statement, node = node.rest
assigns a null
value to node
, there the for loop exits.
The below code does not work because, list.value
may be undefined
, or as a fact any other falsy
value other than null
.
function listToArray(list){
var result = [];
while(list.value != null){
result.push(list.value);
list = list.rest;
}
return result;
};
console.log(listToArray(list));
instead try, while(list.value)
.
...but how does the "node" argument by itself work?
node
is a variable. A variable is resolved to its value. E.g. if I have
var foo = 3;
alert(foo + 1);
it will resolve foo
to the value 3
and then add 1
to it. Similarly in this case, node
is resolved to whatever value it has, that value is converted to a Boolean value (i.e. Boolean(node)
) and then tested whether it is true
or false
.
If the value of node
is null
, then Boolean(node)
will return false
.
why doesnt the following code work?
Can't really tell without knowing what list
is and what exactly "does not work".
But presumably, list.rest
is null
at some point, in which case you would try to access null.value
which throws an error. The equivalent version to the for
loop would be to pare list
itself:
while (list != null)
A simpler way is to use: var result = Array.prototype.slice.call(list);
this will turn the list into an array
function listToArray(listValue){
var arrayResult = [];
while(listValue.list){
arrayResult.push(listValue.value);
listValue = listValue.list;
}
arrayResult.push(listValue.value);
return arrayResult;
}
本文标签: arrayslistToArray Eloquent JavaScriptStack Overflow
版权声明:本文标题:arrays - listToArray Eloquent JavaScript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745477175a2660013.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论