Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

This has been incredibly frustrating, especially given the simplicity of it. Simple javascript:

<script type="text/javascript">

var name = new Array("cat","dog");

document.write(name[1]);

</script>

This script prints: "a" (as in the "a" from "cat"...aka name[0][1]...).

Yet when I change it to document.write(name), it prints the whole array (i.e. "cat,dog"). Why for the love of god is this simple array failing to print the entire string (which should be "dog")?!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
120 views
Welcome To Ask or Share your Answers For Others

1 Answer

The global variable name refers to the existing window.name property, which was used to set the name of the browser window. It converts any value assigned to it to a string:

window.name = [1, 2, 3];
console.log(typeof window.name, window.name);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...