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

Categories

0 votes
122 views
in Technique[技术] by (71.8m points)

javascript - Can't assign an array to the global variable "name"

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

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

1 Answer

0 votes
by (71.8m points)

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);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...