In my day-to-day work i mostly use C# and only use javascript occasionally, so please, javascript Gurus don't judge my questions roughly!
- Array implements the Stack by providing the
pushandpopmethod, butpeekis missing, why? (yes it is trivial to implement, but still) - Array implements Queue but the
operations are named
push-shiftorunshift-popinstead ofenqueueanddequeue, why name them differently? Is this inspired by Python and Ruby? - Why APIs for Array, Stack and Queue are merged into one object, instead of segregating the interface and having different objects for that? Is it because the implementation is cheap?
- Semantically in many languages (C#, C++, Java) an Array is a continuous block in memory and is not re-sizable. On the other hand, the basic collection that allows easy addition of elements is a List (ArrayList or LinkedList or the like). Would it not be better if Array was named a List in javascript?
- How is Array implemented under the hood? Where can I find a very detailed description?
peekreturns the last element, the same aspopbut does not remove it from the stack. let stack = []; stack.peek = function () { return stack[stack.length - 1]; };shiftandunshiftin JS aren't exactly the same asenqueueanddequeuein C#.shiftandunshiftboth operate on the 0th index of the array, whileenqueueadds to the end of the Queue anddequeueremoves at the front. To use a JS array like a queue, you'd either useunshiftandpopORpushandshift. (I hope I didn't get anything mixed up there.)