I want to assign values to an array, but why can"t for-in access the array elements of an empty array?
var arr=new Array(2);
for(var i in arr){
arr[i]=10;
}
I want to assign values to an array, but why can"t for-in access the array elements of an empty array?
var arr=new Array(2);
for(var i in arr){
arr[i]=10;
}
array means that there is no value at a certain position in the array. For example, the array returned by the Array constructor is empty.
Array(3) // [, , ,]
, Array (3) in the code above) returns an array with three empty spaces.
Note that the vacancy is not undefined,. The value of a position equal to undefined, is still valuable. Spaces have no value, as can be illustrated by the in operator.
0 in [undefined, undefined, undefined] // true
0 in [, , ,] // false
the above code shows that the 0 position of the first array has a value, and the 0 position of the second array has no value.
ES5's handling of vacancies is already very inconsistent, and vacancies are ignored in most cases.
// forEach
[,'a'].forEach((x,i) => console.log(i)); // 1
// filter
['a',,'b'].filter(x => true) // ['a','b']
// every
[,'a'].every(x => x==='a') // true
// reduce
[1,,2].reduce((x,y) => x+y) // 3
// some
[,'a'].some(x => x !== 'a') // false
// map
[,'a'].map(x => 1) // [,1]
// join
[,'a',undefined,null].join('-sharp') // "-sharpa-sharp-sharp"
// toString
[,'a',undefined,null].toString() // ",a,,"
ES6 explicitly converts the vacancy to undefined.
for-in statement can traverse the enumerable properties of the object itself and its prototype chain .
here you need to know the attribute descriptor of the object.
the array items (sparse array items) generated through Array (num) cannot be traversed naturally without initializing the generation of the attribute descriptor .
var arr = new Array(2);
Object.getOwnPropertyDescriptor(arr, '0'); // undefined
Object.getOwnPropertyDescriptor(arr, 'length')
// {value: 2, writable: true, enumerable: false, configurable: false}
is faulty.
for.in loop only traverses enumerable properties
because for in
traverses the object's key, empty array is
for in can only traverse enumerable attributes and exist values, and undefined is equivalent to no value.
Previous: The problem of judging multiple scores by multiple custom forms generated dynamically by element
study online tutorials learn to package projects using webpack, a simple code implementation. the entry file index.html of the interface is as follows: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&qu...
refer to antd s custom scheme, introduce babel-plugin-import and react-app-rewired, and add .bablrc file, but not successfully is it necessary to eject? how to make create-react-app support stage-0, without eject? ...
copied a crawler from the website to crawl some product information, but I don t know why the single page of the product just can t climb down. but tested that all other pages are crawable. Why? found the next returned error information, which seems...
1.Angularjs5. Using Angular CLI to build a project to introduce bootstrap,WebStrom bootstrap and angularjs is not prompted. Angularjs has this prompt that the plug-in angularjs has been installed, but the ng- syntax seems to have no hint, and bootStrap ...
what is written in the configuration file directly is the private network url, but now you want to use the environment variables configured on the server instead. How to modify it module.exports = { NODE_ENV: "production" , ENV_CONFIG: ...
find a webstrom activation code. http: idea.codebeta.cn suddenly couldn t use it yesterday ...
I want to make a vue component by myself, but if I want to mention it separately, I can put it in other projects. Other projects use cdn, so I want to make a plug-in like JQ, as long as I reference vue.js and my compiled components. it s a pity that I...
I wrote a command line tool based on webpack , using webpack-dev-server . Modifying the file does not cause page reload or module overload, as shown below: modifying files can cause compiled refresh to see the modified content websocket receive...
when I use the http: www.pss-system.gov.cn . regular search function, I jump to a new page. for example, if I type CN201711262863 for retrieval, I will jump to http: www.pss-system.gov.cn .. I want to know how the following params parameters are...
95% emitting, it can be started normally. What is the reason for this? Will affect later operations? ...
at present, the project is a front-end built with vue.js. I searched the Internet and found only two plug-ins, one is Baidu s echarts, and the other is d3.js. but it seems that the tree structure of these two trees can only be tree spread structure, s...
es6 is set in webstorm : Chrome ...
as shown in the figure, the recognition es6 syntax is set, but import vueAwesomeSwiper form vueAwesomeSwiper always reports an error; and no matter what file is referenced in this way, it will report an error ...
because the sever side belongs to someone else, you can only handle cross-domain requests at the front end axios how to deal with cross-domain problems. My configuration in proxyTable only works locally Cross-domain problems occur again in the...
now there is a need to embed an external site in a background management site as a menu item. Similar to the iframe, click menu, the specified path resources are loaded in the iframe to refresh the local content. But it always feels strange to use iframe...
here s the thing A page has two left and right blocks the left side is connected to the socket line, and the right side is in the polling data when the socket on the left is broken, the data in polling on the right will be unable to request data a...
can I connect two websocket connections to one page? because of the backend data architecture, different data have to be obtained from different servers, so there is this requirement have any buddies ever used the experience of using one page with t...
without Webpack: this simple example is fine. Open it on the browser and you can see "hello " <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=devic...
use webpack dll to package dependent packages and put react into the module packaged by dll. when the packaging is complete, the app.bundle.js still contains the source code of react. Could you tell me what to do with the change? webpack.dll.js modu...
when learning to use the extract-text-webpack-plugin plug-in an error occurs when the extraction file uses contenthash, but there is no error using hash, and chunkhash ...