Javascript Array Wrapper or Class
-
I need a Wrapper or a Class that can control the Max Length of an Array on setting a value... It might be possible with a Prototype too, which is fine...
let myArray = new ArrayClassThing();
myArray.setMax = 50; // should be a const value set in Constructor or Prototype
myArray[50] = "Foo"; // This should work fine, I know index starts at 0, but I am not using 0 so total of 50
myArray[51] = "Bar"; // Need to do nothingJavascript is different than Ruby where you can use special characters to define Getter and Setter methods:
# Ruby Code
class Array_Wrapper
def initialize
@data = []
endGetter
def [](id)
if id <= 50 and @data[id]
return @data[id]
end
endSetter
def []=(id,value)
if id <= 50
@data[id] = value
end
endAs far as I understand, cant use a "[]" as a Getter or Setter Method, and I need something that can do exactly that, just to control a Static Maximum, like in the first code block... Perhaps a better question to ask is how to define Getter and Setter Method WITHOUT A PROXY (which I KNOW is damn well good and possible) of a Class Prototype, but from inside the constructor (which I also know is syntactical sugar)... Can anyone help out with this?