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 8175019
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T22:37:02+00:00 2026-06-06T22:37:02+00:00

Question from Object-Oriented JavaScript book: Imagine Array() doesn’t exist and the array literal notation

  • 0

Question from Object-Oriented JavaScript book: Imagine Array() doesn’t exist and the array literal notation doesn’t exist either. Create a constructor called MyArray() that behaves as close to Array() as possible.

I thought it would be a good challenge to test my skills. This is what I came up with, but it doesn’t work and is very incomplete.. I am a bit stumped:

function MyArray(){

// PRIVATE FIELDS -----------------------

var initialData = arguments;
var storage;

// PRIVATE METHODS ----------------------

function refresh(){ //this doesn't work :(
    for(var i = 0; i < storage.length; i++){
        this[i] = storage[i]
    }
};

function initialize(){
    storage = initialData;
    refresh();
}

function count(){
    var result = 0;
    for(var item in this){
        //console.log(item, parseInt(item), typeof item);
        if(typeof item == 'number'){
            result++;
        }
    }
    return result;
};

initialize();

// PUBLIC FIELDS -------------------------

this.length = count();

// PUBLIC METHODS ------------------------

//todo:
this.push = function(item){
    refresh();
}

this.pop = function(){}

this.join = function(){}

this.toString = function(){}

}

var c = new MyArray(32,132,11);

console.log(c, c.length);

This isn’t for any production code or any project.. just to try to learn JavaScript a lot more. Can anyone try to help me with this code?

  • 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-06T22:37:04+00:00Added an answer on June 6, 2026 at 10:37 pm

    The thing is that you can use arguments object. It’s not an array that was created with Array() so you won’t break rules of the exercise. Here’s what you need to do:

    this.length = 0;
    for (var i in arguments) {
       this[this.length] = arguments[i];
       this.length++;
    }
    

    I forgot to mention that ANY object is an associative array, so it’s not a wrong thing to apply associative arrays for the exercise as we don’t use the Array() object itself.

    To author of the question: in your example you use: this[“i”] = storage[i] that equals to this.i = storage[i]. Try to remove quotes and use it like this[i] = storage[i]

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

Sidebar

Related Questions

From a previous question on Stackoverflow Iterating through/Parsing JSON Object via JavaScript .... My
I got this example demonstrating AJAX from Stoyan Stefanovs Object Oriented JavaScript at page
A question about the flow of information in an object oriented construction, e.g. from
I know we can load a Object from .class file and my question is
The question from title doesn't need more details.
I have a question re object-oriented design - I have two classes, a Map
Java has primitive data types which doesn't derive from object like in Ruby. So
Simple question for all you pragmatic object-oriented fellas. I have read many times to
I've been thinking about this object oriented design question for a while now and
I want to ask a question about how you would approach a simple object-oriented

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.