The question is this:
Create a page with a number of links. Then write code that fires on the window onload event, displaying
the href of each of the links on the page.
And this is my solution
<html>
<body language="Javascript" onload="displayLink()">
<a href="http://www.google.com/">First link</a>
<a href="http://www.yahoo.com/">Second link</a>
<a href="http://www.msn.com/">Third link</a>
<script type="text/javascript" language="Javascript">
function displayLink()
{
for(var i = 0;document.links[i];i++)
{
alert(document.links[i].href);
}
}
</script>
</body>
</html>
This is the answer provided by the book
<html>
<head>
<script language=”JavaScript” type=”text/javascript”>
function displayLinks()
{
var linksCounter;
for (linksCounter = 0; linksCounter < document.links.length; linksCounter++)
{
alert(document.links[linksCounter].href);
}
}
</script>
</head>
<body onload=”displayLinks()”>
<A href=”link0.htm” >Link 0</A>
<A href=”link1.htm”>Link 2</A>
<A href=”link2.htm”>Link 2</A>
</body>
</html>
Before I get into the javascript tutorial on how to check user browser version or model,I was using the same method as the example,by acessing the length property of the links array for the loop,but after I read through the tutorial,I find out that I can also use this alternative ways,by using the method that the test condition will evalute to true only if the document.links[i] return a valid value,so does my code is written using the valid method??If it’s not,any comment regarding how to write a better code??Correct me if I’m wrong,I heard some of the people say “a good code is not evaluate solely on whether it works or not,but in terms of speed,the ability to comprehend the code,and could posssibly let others to understand the code easily”.Is is true??
In general, when looping through an array, you want to use its
lengthproperty as the book’s solution did. Your solution should be just fine in this particular situation as well, but it has a weakness: It’s perfectly valid for an entry in an array to be0,null,undefined, orfalse, all of which are “falsey” values and sodocument.links[i]could be false even though you weren’t at the end of the array.Example:
That will alert
3,2, and1, but then stop. Compare to:…which will alert
3,2,1,0,-1, and-2.You might see code that looks like this:
for (index in a). In general don’t use that to loop through array indexes, it’s based on a misconception of whatfor..indoes. (More on that below.)(There is another, new way of looping through array entries has been added as of the new 5th edition specification: The
forEachfunction. You give it a function, and it calls it for each element in the array. Details in this other answer.. Sadly, IE8 doesn’t support it, but it’s one of the things that can be “shimmed” — search for “es5 shim” for multiple options.)When learning about arrays, it’s important to know that Javascript arrays are very different from arrays in most other languages. For one thing, they’re not (necessarily) arrays at all; in fact, they’re just normal Javascript objects with a couple of special features added on. Javascript objects are property maps, they map keys to values. For instance:
That
objobject maps the key"foo"(a string) to the value1. You can access that property either by using a literal name in your code, or by using[]and a string. And of course, if you’re doing the latter, you can use any string (literal or from a variable or from an expression, etc.). So all of these have the same result:…you get the idea; as long as what you use within the
[]evaluates to a string, you can look up the value using that key. But Javascript also does implicit coercion, so this works:There I’ve mapped a property named
"1"to the value"one". But then I look it up withobj[1], using a number rather than a string. That’s okay, the interpreter will turn it into a string for me and then do the key lookup.What does all of this have to do with arrays? This: Array indexes are just property names. A Javascript array is a normal object that maps keys to values, with these special features:
Whenever you set a property whose name can be interpreted as a number, if that number is greater than the current maximum index present in the array, the
lengthproperty is changed. So:Whenever you set
length, if there are properties with numeric names that have a value greater than or equal to the new length, those properties are deleted from the object.They have various properties for functions they inherit from the Array.prototype, like
joinorsplice.That’s it. Nothing at all like arrays in C, C++, Java, or most other languages.
Since arrays are just objects with a couple of extra features, you can put other, non-numeric properties on arrays if you want to:
And this is where the
for..inthing breaks down: Becausefor..indoes not loop through array indexes, it loops through property names:This alerts
"1","2","3", and"foo"(in no particular order). You can see how if you’d assumed that it did just array indexes, you’d be in trouble! You can use it to loop array indexes, but it’s more complicated than it’s worth:That first checks to see if the property name is a number, and then checks to see that the property is defined on
aitself, not the Array.prototype (remember that arrays inherit properties from the Array prototype). (To be fair, this latter check is probably not all that important; if someone is adding numerically-named properties to the Array prototype, they’re doing a very Bad Thing(tm).)