This is the 20th day of my participation in the August More Text Challenge.
The Proxy to the array
var arr = new Proxy([] and {get: function(obj, prop) {
return obj[prop] += 1;
},
set: function(obj, prop, value) {
obj[prop] = value + 1
return true; }}); arr[0] = 0;
console.log(`arr.length = ${arr.length}`)
console.log(`arr.length = ${arr.length}`)
console.log(`arr.length = ${arr.length}`)
arr[1] = 1;
console.log(`arr.length = ${arr.length}`)
Copy the code
Answer: 2, 5-tetrafluorobenzoic
instructions
// Initialize a proxy array arr
// Default arr = []
var arr = new Proxy([] and {get: function(obj, prop) {
// Get the array arr attribute by +1
return obj[prop] += 1;
},
set: function(obj, prop, value) {
// Set the array arr property by +1
obj[prop] = value + 1
return true; }}); arr[0] = 0; / / arr. At this time length
// When reading the length of the arr attribute, the preceding proxy is incremented by 1
Arr. Length = 2, arr=[1, null]
console.log(`arr.len = ${arr.length}`)
Arr. Length = 3, arr=[1, null, null]
console.log(`arr.len = ${arr.length}`)
Arr. Length = 4, arr=[1, null, null, null]
console.log(`arr.len = ${arr.length}`)
Arr [1] = 1, arr=[1, 2, null, null]
arr[1] = 1;
[1, 2, null, null, null]
console.log(`arr.len = ${arr.length}`)
Copy the code
JS object and array properties
// What is the result of executing the following code
var arr = ['a'.'b'.'c'.'d'];
var obj = { a: 1.b: 2.c: 3.d: 4 };
var arrProperties = Object.getOwnPropertyNames(arr);
var objProperties = Object.getOwnPropertyNames(obj)
console.log(arrProperties.length === objProperties.length)
Copy the code
Answer: false
instructions
- Object. GetOwnPropertyNames can get Object’s properties and native built-in attributes
- Array objects have the length native built-in property
- Object.getOwnPropertyNames(arr) = [“0”, “1”, “2”, “3”, “length”]
- Object.getOwnPropertyNames(obj) = [“a”, “b”, “c”, “d”]
An array of the filter
// What is the result of executing the following code
var arr = [0.1];
arr[4] = 5;
arr[5] = 6;
var arr2 = arr.filter((i) = > {
return i;
})
console.log(arr.length);
console.log(arr2.length);
Copy the code
Answer: 6 and 3
instructions
- Filter iterates through the contents of the array and passes the elements of the array in the callback argument
- The filter callback returns a new array
- Indexes in an array that are deleted or never assigned are not called
console.log(arr) //[0, 1, <2 empty items>, 5, 6], so the length of arR is 6
console.log(arr2) //[1, 5, 6], so the length of arr2 is 3
Copy the code