I need help understanding how to use the Array concat() method and how to write pure JavaScript code according to the ECMA-262 standard.
- Assume O is the object we are working with.
- Let A be the new array created based on O with a length of 0.
- Set the initial count n to 0.
- Create a list called items, with the first element being O and the rest of the elements being the arguments passed to the function.
Iterate through items until it is empty:
a. Take the first element from items and assign it to E.
b. Check if E can be spread.
c. If E can be spread:
i. Set k to 0. ii. Get the length of E and ensure it does not exceed the maximum length. iii. If adding the length of E to n exceeds the limit, throw an exception. iv. Repeat until k is less than the length of E: 1. Get the property at index k from E. 2. If the property exists in E: a. Get the value of the property. b. Add the value to the new array A. 3. Move to the next index in the new array A. 4. Move to the next index in E.
d. If E cannot be spread:
i. If adding E to the new array exceeds the limit, throw an exception. ii. Add E to the new array A. iii. Move to the next index in the new array A.
- Set the length of the new array A to n.
- Return the new array A.
I want to write code that looks like this
Array.prototype.concatCustom = function(){
let 0 = ;
let A = ;
let n = 0;
let item = ;
// more code...
}
let arr1 = [1,2];
let arr2 = [3,4];
arr1.concatCustom(arr2) // achieve the same result as concat()