This is code from Crockford's JavaScript: The Good Parts.
var results = [];
var walkDOM = function (node,func) {
func(node); //What does this do?
node = node.firstChild;
while(node) {
walkDOM(node,func);
node = node.nextSibling;
}
};
I understand the code except for func(node)
. I guess the point is to pass node
as a parameter in function func
, but how will the browser understand it this way? node
and func
could be anything--so when the function is called it could read like this:
walkDOM(document.body,function(att) {
node.getAttribute(att);
results.push(node);
});
When func
is passed, walkDOM
will process function(att) {...}(document.body)--which wouldn't make any sense. So why has Crockford chosen to include func(node)?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…