admin管理员组文章数量:1435208
Okay, so I am just going over some basic programming tenets in JavaScript (I am new to programming, so bear with me please). Below is the code I am having issues with (pay particular attention to the string ponent of the array).
var name = new Array();
var sales = new Array();
var total = prompt("How many total salesmen does your department employ?");
for(i = 0; i < total; i++)
{
name[i] = prompt("What is his/her (the salesman's) name?");
sales[i] = prompt("How much (in dollars) did he/she sell?");
}
for(j = 0; j < i; j++)
{
document.write(name[j]+" had $"+ sales[j]+" in sales!<br>");
}
Okay, so I am just going over some basic programming tenets in JavaScript (I am new to programming, so bear with me please). Below is the code I am having issues with (pay particular attention to the string ponent of the array).
var name = new Array();
var sales = new Array();
var total = prompt("How many total salesmen does your department employ?");
for(i = 0; i < total; i++)
{
name[i] = prompt("What is his/her (the salesman's) name?");
sales[i] = prompt("How much (in dollars) did he/she sell?");
}
for(j = 0; j < i; j++)
{
document.write(name[j]+" had $"+ sales[j]+" in sales!<br>");
}
As you can see, it's just a simple code to get me familiar with arrays of various types of elements. For some reason, the code returns:
undefined had $100 in sales!
undefined had $2999 in sales!
undefined had $4999 in sales!
undefined had $32342 in sales!
which is exactly what I need, except for the fact that each of the elements within the name array is undefined.
I'm thinking it may be an issue with the document.write function, as i've read that it can be dubious but I still don't know exactly how to get this working properly.
Any help would be greatly appreciated!
Share Improve this question edited Jun 24, 2024 at 4:47 dumbass 27.3k4 gold badges38 silver badges74 bronze badges asked Dec 31, 2014 at 2:32 Sam S.Sam S. 3661 gold badge4 silver badges16 bronze badges 7- 2 Works just fine for me -> jsfiddle/k52betc3 – adeneo Commented Dec 31, 2014 at 2:35
- 1 @adeneo That jsfiddle is broken for me in Chrome – user229044 ♦ Commented Dec 31, 2014 at 2:38
- 1 @adeneo me too, im running Version 39.0.2171.95 m of Chrome – Master Yoda Commented Dec 31, 2014 at 2:39
-
3
name
is your problem, and also the reason it works for some people in JSFiddle. JSFiddle wraps your code in a function before its invoked. – user229044 ♦ Commented Dec 31, 2014 at 2:39 - Ahhhh.... that fixed the issue. A simple variable name-switch did the trick. Thanks for all the ments, that worked beautifully. – Sam S. Commented Dec 31, 2014 at 2:44
3 Answers
Reset to default 6name
is a property of window
. Your function won't work at global scope. The simpler example which reproduces the problem would be...
var name = {}
name.blah = "blah"
document.write(name.blah); // undefined
Wrap it in a function, or choose a different variable name. names
is more appropriate any way, since it's an array of multiple objects, you should choose a plural name for the variable:
var names = new Array();
var sales = new Array();
var total = prompt("How many total salesmen does your department employ?");
for(i = 0; i < total; i++)
{
names[i] = prompt("What is his/her (the salesman's) name?");
sales[i] = prompt("How much (in dollars) did he/she sell?");
}
for(j = 0; j < i; j++)
{
document.write(names[j]+" had $"+ sales[j]+" in sales!<br>");
}
I can reproduce the behavior you're describing in Chrome.
I think it's because of a subtlety with global variables in JavaScript. If you're in top-level code -- not inside a function -- and declare a variable, that's treated as a global variable, which means it's actually a property of the window
object. There's already a property called window.name
, which is predefined by the browser, so your attempt to make a global variable called name
produces some unexpected behavior.
If you rename your name
variable to names
, it works as expected. (And since it's a bunch of names, names
is a better name anyway.)
Alternatively, you could wrap the whole thing in a function and call it immediately. Then you have local variables (which really are variables), rather than global variables with their weird behavior:
(function() {
var name = new Array();
var sales = new Array();
var total = prompt("How many total salesmen does your department employ?");
for(i = 0; i < total; i++)
{
name[i] = prompt("What is his/her (the salesman's) name?");
sales[i] = prompt("How much (in dollars) did he/she sell?");
}
for(j = 0; j < i; j++)
{
document.write(name[j]+" had $"+ sales[j]+" in sales!<br>");
}
})();
name
refers to windows.name when javascript is run in browser.
Using another variable name fixes the problem
What is the `name` keyword in JavaScript?
本文标签: javascriptWhy can’t an array variable named name be used normallyStack Overflow
版权声明:本文标题:javascript - Why can’t an array variable named `name` be used normally? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745648913a2668304.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论