Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. JavaScript
  4. Javascript Array Wrapper or Class

Javascript Array Wrapper or Class

Scheduled Pinned Locked Moved JavaScript
questionjavascriptrubydatabasedata-structures
1 Posts 1 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Member_15100276
    wrote on last edited by
    #1

    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, 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?

    1 Reply Last reply
    0
    Reply
    • Reply as topic
    Log in to reply
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes


    • Login

    • Don't have an account? Register

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • World
    • Users
    • Groups