I'm tryig to create a array named status in my javascript, but it is not working in Google Chrome.
<html>
<body>
<script>
var array = [1, 2, 3];
document.write("Type of开发者_运维问答 [" + array + "] : " + (typeof array) + "<br />");
document.write("Value of array.length : " + array.length + "<br />");
document.write("<br /><br />");
var status = [1, 2, 3];
document.write("Type of [" + status + "] : " + (typeof status) + "<br />");
document.write("Value of status.length : " + status.length + "<br />");
</script>
</body>
</html>
In the above piece of code even though I'm assigning an array value to the variable status In chrome the value is considered as of type string.
Is it a bug with Chrome or a valid behaviour?
The problem is that in Chrome, window
(the global object) has a status
property, which for some reason Chrome seems to be referring to instead of your status
var. Renaming status
to anything else, say, myStatus
, will give you the results you expect.
This issue is already answered here.
<html>
<body>
<script>
function test(){
var array = [1, 2, 3];
document.write("Type of [" + array + "] : " + (typeof array) + "<br />");
document.write("Value of array.length : " + array.length + "<br />");
document.write("<br /><br />");
var status = [1, 2, 3];
document.write("Type of [" + status + "] : " + (typeof status) + "<br />");
document.write("Value of status.length : " + status.length + "<br />");
}
test();
</script>
</body>
</html>
If you try the above given code it works as expected because the scope of the variable status is limited to the local scope of the method status. In your sample the scope of the variable was global(scope is window).
In javascript, I think you want:
var array = new Array(1, 2, 3);
精彩评论