Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8603193
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T02:16:00+00:00 2026-06-12T02:16:00+00:00

I want an object class in javascript that will be written in a separate

  • 0

I want an object class in javascript that will be written in a separate js file.

This object class will be called Pages.

the purpose of this object class is for me to manipulate the files property in html5 which is purely read-only.

See http://help.dottoro.com/ljslrhdh.php

I want the following properties/functions in Pages

  • Pages.length : is a read-only property.
  • Pages.add(key, value) : is a function that will do an add like an associative array
  • Pages.getByKey(key) : is a function that will return the value associated with the key
  • Pages.getByIndex(index) : is a function that will return the value associated with the index
  • Pages.removeAll() : is a function that will remove all the key-value pairs and therefore the length will be zero.
  • Pages.removeByKey(key) : is a function that will remove the corresponding key-value pair
  • Pages.removeByIndex(index) : is a function that will remove the corresponding key-value pair
  • a constructor
  • Pages.createFrom(files) : returns an instance of Pages object that will automatically create based on the files object as stated in the link above.
  • Pages.indexExists(index) : returns boolean if there is such an index
  • Pages.keyExists(key) : returns bookean if there is such a key-value pair

The most important characteristics are that:

  1. whenever I add new key-value pair, it will be appended to the end of the Pages object.
  2. the key-value pair can be accessed by either the key using .getByKey(key) or the .getByIndex(index) E.g., the first key-value pair can be accessed by index 0.
  3. whenever any existing key-value pair is removed or added, the length property is updated AND the indices are updated as well. E.g., if there are 5 key-value pairs and I remove the 2nd one, the 3rd key-value pair now can be accessed using the index 1 and so on.

I do not need code for the various functions.

I just need a skeleton structure of creating the above custom object class in javascript

I read Set length property of JavaScript object and thought I need to do it as a function.

But then I saw some answers suggesting various improvements like this https://stackoverflow.com/a/6412732/80353 and https://stackoverflow.com/a/6412869/80353 about using Object.create

so I am asking for the best template going forward, so that I can add new functions when needed.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T02:16:02+00:00Added an answer on June 12, 2026 at 2:16 am

    Here is the barebones of a structure I have used before, I have only tested this on the latest browsers – however it isn’t using any techniques that should cause a problem. The only possible contention would be prototyping an object with an Array. But I don’t see why this wouldn’t work in older browsers:

    <script>
      "use strict";
    
      var ArrayLike = (function(){
        /// create a static reference to our constructor so that we can
        /// add methods to ArrayLike... if we like.
        var _static = function(){
          /// store some "private" values, and arrayify our arguments
          var _args = Array.prototype.slice.call( arguments ),
              _private = { byKey:{}, byIndex:{} }, 
              _public = this;
          /// make sure the user used the 'new' keyword.
          if ( _public instanceof _static ) {
            /// if we have arguments then push them onto ourselves
            if ( _args.length ) {
              _public.splice.apply(_public,[0,0].concat(_args));
            }
            /// Now that a new instance has been created, switch in an array 
            /// prototype ready for the next instance.
            _static.prototype = new Array();
            /// start adding our methods, bare in mind if you wish to
            /// stop any of the native array methods from working you'll 
            /// have to override them here.
            _public.add = function( key, value ){
              /// store the keys and indexes as references to themselves.
              _private.byKey[key] = _public.length;
              _private.byIndex[_public.length] = key;
              /// use the inherited push function from the array.
              _public.push( value );
            }
            /// an example function to show how get by key would work.
            _public.getByKey = function(key){
              if ( (key = _private.byKey[key]) || key === 0 ) {
                return _public[key] ? _public[key] : null;
              }
            }
            /// easy removeAll thanks to the array prototype.
            _public.removeAll = function(){
              _public.length = 0;
            }
            /// here I leave you to flesh out the methods that you 'want'.
            _public.removeByKey = function(){
    
            }
            /// I'll give you a clue that keeping your array index in order
            /// is going to be a manual process, so whenever you delete you
            /// will have to reindex.
            _private.reIndex = function(){
    
            }
          }
        }
        /// set-up the prototype as an array ready for creation
        _static.prototype = new Array();
        /// return the function that will be our constructor
        return _static;
      })();
    
    </script>
    

    The above is a bit odd from the point of view of a normal constructor, because it is constantly modifying it’s prototype, this means the following doesn’t work as expected:

    var a = new ArrayLike(1,2,3);
    alert( a instanceof ArrayLike ); /// alerts FALSE
    

    The benefits of extending from an Array are quite obvious though as you can now treat a like any array – so some of your work is done for you by core JS code. As you are implementing a system that uses keys however, it may be best to override most of the normal array operations so that you can keep a proper track of the keys that are in use in within the structure.

    Anyway hope it helps, I’ve left you to work out the slightly tricky elements of reindexing the array as it should be straight forward with the way the above is setup.

    Other enhancements

    With regards to setting certain properties to read-only, this is only truly possible in JavaScript 1.8+ – so it wont be backwards compatible to older browsers. You can achieve this using Object.defineProperty(obj, prop, descriptor) as mentioned in Felix Kling’s comment. Using this it should be possible to affect things like .length and make them read-only. However, the more locked down you make your code, the less flexible and extensible it will be.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to create a Javascript class/object that allow me to have various method:
I know that in OOP you want every object (from a class) to be
Like I would want to do something like this, class Object { public: World
So I want to build a form validation class/object in javascript. The way I
Possible Duplicate: Calling base method using JavaScript prototype I want to inheritance object that
I am having a class Student. I want to store the object of class
Here is what I want to do: class demo(object): def a(self): pass def b(self,
I want to know the name of a given object in a class class
I simply want to change a variable of an object from another class. I
I want to create an object, let's say a Pie. class Pie def initialize(name,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.