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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T14:50:28+00:00 2026-05-30T14:50:28+00:00

How to get the reference to a slice of an array? var A =

  • 0

How to get the reference to a slice of an array?

var A = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j'];
A.mySlice = function(l, h){return this.slice(l,h)};

var B = A.mySlice(1,5); // ["b", "c", "d", "e"]

It works for direct slices derived from A. But, how to get it for all slices derived? (in this case for B)

B.mySlice = function(l, h){return this.slice(l,h)};

A[3] = 33;
A.mySlice(1,5) // ["b", "c", 33, "e"] => ok
B.mySlice(0,3) // ["b", "c", "d"] => I would want ["b", "c", 33]
  • 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-05-30T14:50:29+00:00Added an answer on May 30, 2026 at 2:50 pm

    I don’t think you can do this with native JS arrays (well, not in a straightforward manner anyway).

    I think the cleanest approach would be going back and using custom objects to represent the slices. Perhaps something along these lines:

    function ArraySlice(arr, lo, hi){
        this.arr = arr;
        this.lo = lo;
        this.hi = hi;
        this.length = hi - lo;
    };
    ArraySlice.prototype._contains = function(ix){
        return this.lo + ix < this.hi;
    };
    ArraySlice.prototype.get = function(ix){
        if (this._contains(ix)){
            return this.arr[this.lo + ix];
        }else{
            return; //undefined
        }
    };
    ArraySlice.prototype.set = function(ix, value){
        if (this._contains(ix)){
            return (this.arr[this.lo + ix] = value);
        }else{
            return; //undefined
        }
    };
    
    var a = [0,1,2,3,4,5];
    var b = new ArraySlice(a, 1, 3);
    
    a[2] = 17;
    console.log( b.get(1) );
    

    Of course, this loses the convenient [] syntax and the objects aren’t arrays anymore but subclassing Array is annoying and error prone and I don’t believe there is a cross-browser way to do operator overloading.

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

Sidebar

Related Questions

How can I get a reference to a module from within that module? Also,
I am making a reference to the Javascript function splice() on an array and
I do not want any code but want to get reference that how can
I do not want any code but want to get reference that how can
I do not want any code but want to get reference for MKMapView compass
When I get a reference to a System.Diagnostics.Process , how can I know if
How do I get the reference to a folder for storing per-user-per-application settings when
How would you get a reference to an executing class several stack frames above
How can I get a reference to the directory of the visual studio project
How do I get Assembly reference for a .dll with no types in it?

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.