Heretic86 Posted May 12, 2021 Share Posted May 12, 2021 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 nothing Javascript is different than Ruby where you can use special characters to define Getter and Setter methods: # Ruby Code class Array_Wrapper def initialize @data = [] end # Getter def [](id) if id <= 50 and @data[id] return @data[id] end end # Setter def []=(id,value) if id <= 50 @data[id] = value end end As 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 Can anyone help out with this? Quote Link to comment https://forums.phpfreaks.com/topic/312672-array-wrapper-or-class/ Share on other sites More sharing options...
requinix Posted May 12, 2021 Share Posted May 12, 2021 Take a look at Proxy. Quote Link to comment https://forums.phpfreaks.com/topic/312672-array-wrapper-or-class/#findComment-1586488 Share on other sites More sharing options...
Heretic86 Posted May 12, 2021 Author Share Posted May 12, 2021 (edited) Im gonna need a lot of classes and trying to keep code clean and minimal. Proxies just make a basic class look... well, messy. What about something like this? class Game_Var { constructor(){ this._data = [null]; const _self = this; _self.__proto__.value = function(id){ // do stuff here; } } } How do I define Getters and Setters in this? Or something similar... Edit: Let me be clear here. I dont want to call the property name from the outside, only the class instance and braces: //... some class definition var foo = new MyClass(); foo[1] = "bar"; if (foo[1] == "bar") alert("ok here"); Edited May 12, 2021 by Heretic86 Quote Link to comment https://forums.phpfreaks.com/topic/312672-array-wrapper-or-class/#findComment-1586490 Share on other sites More sharing options...
requinix Posted May 12, 2021 Share Posted May 12, 2021 Worry about "clean" and "minimal" once you have stuff that works, first. Quote Link to comment https://forums.phpfreaks.com/topic/312672-array-wrapper-or-class/#findComment-1586491 Share on other sites More sharing options...
Heretic86 Posted May 12, 2021 Author Share Posted May 12, 2021 Telling me what my priorities are isnt helping to make sure it works. And I am not using a proxy to do something that is part of the syntactical sugar of the language. If you dont know, say you dont know, because what you are telling me is not helping. Quote Link to comment https://forums.phpfreaks.com/topic/312672-array-wrapper-or-class/#findComment-1586506 Share on other sites More sharing options...
requinix Posted May 12, 2021 Share Posted May 12, 2021 I know what the answer is. It's to wrap your Array in a Proxy. If you don't like that answer then does that mean you want to shop around until you find one you like better? Quote Link to comment https://forums.phpfreaks.com/topic/312672-array-wrapper-or-class/#findComment-1586509 Share on other sites More sharing options...
Heretic86 Posted May 12, 2021 Author Share Posted May 12, 2021 No it is not. I am thankful for this site but your answers are not helpful, it should be part of standard class definition using syntax that I am unfamiliar with. Quote Link to comment https://forums.phpfreaks.com/topic/312672-array-wrapper-or-class/#findComment-1586513 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.