为什么array [length]返回列表的第一个元素?

时间:2016-09-11 12:21:33

标签: javascript arrays

我知道可以使用

返回数组的长度

export default class Weather extends Component { getWeather () { var self = this; fetch('http://api.openweathermap.org/data/2.5/weather?zip=sl44jn,uk&units=metric&APPID=ed066f80b6580c11d8d0b2fb71691a2c') .then (function (response) { if (response.status !== 200) { console.log('Looks like there was a problem. Status Code: ' + response.status); return; } response.json().then(function(data) { self.setWeather(data); }); }) .catch (function (err) { console.log('Fetch Error :-S', err); }); } setWeather (forecast) { console.log(forecast); this.setState({ forecast: forecast.name }) } componentWillMount () { this.getWeather(); } componentDidMount () { // window.setInterval(function () { // this.getWeather(); // }.bind(this), 1000); } render() { return ( <h1>{this.state.forecast}</h1> ) } }

array.length

但是省略了这样的引号

array["length"]

将返回数组的第一个元素 这是实际的代码: var list = [5,7,10];然后在控制台列表中[length]返回5,并且没有我在窗口中没有iframe或框架,所以window.length或者只是长度在consol中返回0。

2 个答案:

答案 0 :(得分:4)

您正在使用window.length

  

返回窗口中的帧数(<frame><iframe>元素)。

var array = [21, 22];

console.log(length);
console.log(window.length);
console.log(array[length]);
<iframe></iframe>

答案 1 :(得分:2)

长度等于数字0,如果它存在,则返回第一个元素。

数字0,因为您有浏览上下文(文档)而没有其他上下文。

对nodejs,NW.js等其他环境的认知可能存在,可能与浏览器上下文不同。