当迭代值时,为什么typeof(value)返回“string”当价值是一个数字?使用Javascript [英] When iterating over values, why does typeof(value) return "string" when value is a number? Javascript

查看:167
本文介绍了当迭代值时,为什么typeof(value)返回“string”当价值是一个数字?使用Javascript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Google Chrome进行这个测试:
与直觉相反,第一个循环提示字符串3次,第二个循环提示数字3次。

  numarray = [1,2,3]; 

// for-each循环
(numarray中的数字)
alert(typeof(num));

//标准循环
(i = 0; i alert(typeof(numarray [i]));

我希望两个循环都能提醒数字3次。如何在Javascript中实现第一个循环?换句话说,如果for-each是语法糖,那么使用标准循环是什么呢?

一个标准的循环?我正在寻找使用第二种循环来触摸某个对象的每个方法和属性。

解决方案

返回string的原因返回在你的第一个循环中, num 指向数组索引,而不是 numarray 在那个指数。尝试改变你的第一个循环来提醒 num 而不是 typeof num ,你会发现它吐出了0,1 ,和2,这是指示,而不是你的数组的值。



当您在中使用循环,你迭代了一个对象的属性,在第二个例子中,它不完全相当于循环的。 JavaScript中的数组实际上只是具有序号作为属性名称的对象。对于 typeof 而言,它们被视为字符串。



编辑:



正如Matthew所指出的,在时,不能保证以任何特定顺序获取数组中的项$ c> loop,部分出于这个原因,不建议以这种方式遍历数组。



filip-fku询问什么时候使用 for ,给出这种行为。一个例子是当属性名称本身具有含义时,数组标记并不是这种情况。例如:

  var myName = {
first:'Jimmy',
last:'Cuadra'
};

for(var prop in myName){
console.log(prop +':'+ myName [prop]);

$ b $ //打印:
//第一个:Jimmy
//最后一个:Cuadra

值得注意的是循环中的也会迭代对象原型链的属性。出于这个原因,这通常是你想要在循环中构造一个

 ($ obb){
if(obj.hasOwnProperty(prop)){
//做某事
}
}

检查属性是否由对象本身定义,而不是通过原型继承的对象连锁。


I'm using Google Chrome for this test: Contrary to intuition, the first loop alerts "string" 3 times, while the second loop alerts "number" 3 times.

numarray = [1, 2, 3];

//for-each loop
for(num in numarray) 
    alert(typeof(num));

//standard loop
for(i=0; i<numarray.length; i++) 
    alert(typeof(numarray[i]));

I was expecting both loops to alert "number" 3 times. How is the first loop implemented in Javascript? In other words, if the for-each is syntactic sugar, what is its equivalent using a standard loop?

Also, is there some way to iterate over an object's namespace using a standard loop? I'm looking to touch every one of some object's methods and attributes using a loop of the second kind. I'm new to Javascript and any help is highly appreciated, thanks.

解决方案

The reason you're seeing "string" returned in your first loop is that num refers to the array index, not the value of numarray at that index. Try changing your first loop to alert num instead of typeof num and you'll see that it spits out 0, 1, and 2, which are the indicies and not the values of your array.

When you use a for in loop, you're iterating over the properties of an object, which is not exactly equivalent to the for loop in your second example. Arrays in JavaScript are really just objects with sequential numbers as property names. They are treated as strings as far as typeof is concerned.

Edit:

As Matthew points out, you're not guaranteed to get the items in the array in any particular order when using a for in loop, and partly for that reason, it's not recommended to iterate through arrays that way.

filip-fku asks when it would be useful to use for in, given this behavior. One example is when the property names themselves have meaning, which is not really the case with array indicies. For example:

var myName = {
  first: 'Jimmy',
  last: 'Cuadra'
};

for (var prop in myName) {
  console.log(prop + ': ' + myName[prop]);
}

// prints:
// first: Jimmy
// last: Cuadra

It's also worth noting that for in loops will also iterate through properties of the object's prototype chain. For that reason, this is usually how you'd want to construct a for in loop:

for (var prop in obj) {
  if (obj.hasOwnProperty(prop)) {
    // do something
  }
}

This does a check to see if the property was defined by the object itself and not an object it's inheriting from through the prototype chain.

这篇关于当迭代值时,为什么typeof(value)返回“string”当价值是一个数字?使用Javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆