Javascript 不是数组也不是字符串,数据类型是什么?

Javascript 不是数组也不是字符串,数据类型是什么?,javascript,vector,initialization,counter,items,Javascript,Vector,Initialization,Counter,Items,我在js中找到了一个脚本,但我不明白变量“n”是什么类型的数据。 剧本是: var buttons= document.getElementByTagName("button"); var n= buttons.length, counters=[]; //initialize the vector of counters counters.length= n; counters.______ (0); 变量n中有一个向量和一个整数 另一个问题,这个练习是要完成的,我可以在最后一行写c

我在js中找到了一个脚本,但我不明白变量“n”是什么类型的数据。 剧本是:

var buttons= document.getElementByTagName("button");

var n= buttons.length, counters=[];

//initialize the vector of counters

counters.length= n;

counters.______ (0);
变量n中有一个向量和一个整数

另一个问题,这个练习是要完成的,我可以在最后一行写counters.items(0)吗,或者我可以使用“items()”,括号内只有两个数字


谢谢

如果您想使用计数器获取页面上的所有按钮,计数器将变得可用,因为我们将集合转换为数组,您可以从中计数,这是一个更好理解的示例,请尝试以下操作:

var buttons = document.getElementsByTagName("button"); // here the var buttons become a collection of html elements
var numberOfElements = buttons.length; // here you get the size of the collection
var collectionOfElements = [].slice.call(buttons); //since getElementsByTagName will return a collection of html elements you need to transform them to array

console.log("Number of elements: " + numberOfElements);
console.log(collectionOfElements); // printing the array

var n=buttons.length
.length
包含一个数字,因此n是一个数字
getElementByTagName
应该是
getElementsByTagName
@CertainPerformance我不知道你写的第二个“.length”。“counter=[]”是空数组吗?我怎么可能有一个包含另一个数字和一个数组的数字?我想象变量n是这样的:var n=6,[2,5,1,9];其中6是按钮长度,2,5,1,9是数组“counters”的项,谢谢,但这是一个完成练习,所以我无法更改脚本:)@Ale17请解释一下,您需要用整数填充计数器吗?我不明白,我也不认识我,这是我老师在一次旧测验中写的剧本,我必须完成它。在评论行中,他告诉“counters”是一个vector@Ale17js中的向量只是数组,您的示例编写时有错误,您无法完成某些无法按原样运行的操作is@Ale17例如,var n=buttons.length,counters=[];我想你想把n变成一个物体?但这永远不会起作用,为了使它成为一个对象,您需要编写var n={length:buttons.length,counters:[]};等等