A JavaScript Proxy allows-intercept operations performed on objects, arrays, or functions like property lookup, the Ignment, invocation, property deletion, and more to add custom behavior. In this lesson we looks at what intercept property lookup with get
the "trap", that'll allow us to get items starting From the end of the array with ease.
console.clear ()ConstCharacters = [ 'Harry Potter', 'Ron weasly', 'Hermione Granger', 'Nevel Longbottom', 'Lavender Brown', 'Scabbers', 'Pigwidgeon',]ConstHandler = { //target:the Array itself//name:the Index which passed in Get(target, name) {//Check whether index is 0,1...6 if(Nameinchtarget) { //If yes, then get the value back returnReflect.Get(target, name)}Else { //if not, then the name Is-1,-2, -3 ... Constindex =Number (name); returnReflect.Get(Target, Target.length +index)} }}ConstProxy =NewProxy (characters, Handler) Console.log (proxy[3]);//Nevel LongbottomConsole.log (proxy[0]);//Harry PotterConsole.log (proxy[-2]);//Scabbers
[ES6] Use ES6 Proxies