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

  • Home
  • SEARCH
  • 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 8462043
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T14:03:31+00:00 2026-06-10T14:03:31+00:00

I want to create an object such that object.a is an array while object.b

  • 0

I want to create an object such that object.a is an array while object.b is the length of it and this is what I have:

var object = {
   a:[1,2,3,2],
   b: this.a.length
}
alert(b);

But this won’t alert 4. There is something wrong with b: this.a.length:

Uncaught TypeError: Cannot read property 'length' of undefined

I’d be glad if someone can explain to me why this is happening.

It would actaully alert 4 if I write it this way:

 var object = {
       a:[1,2,3,2]
 }
    alert(object.a.length);

[update] even create the object this way, it doesn’t work:

var object = {
   a:[1,2,3,2],
   b:object.a.length
}
  • 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-10T14:03:32+00:00Added an answer on June 10, 2026 at 2:03 pm

    The main problem is that in javascript data declaration like your first example, the value of this is not set to the object being defined. So since this is not your object, this.a inside the object declaration does not refer to the a property of your object.

    It is whatever it was in the context right before your data declaration. If that was the global context, then this would be the window object and this.a probably doesn’t exist. If it doesn’t exist (e.g. it’s undefined), then when you try to reference this.a.length, you are trying to read the property .length off something isn’t an object and that is a javscript error.


    As for solving this problem it is generally a bad idea to copy the length of an array to another property. That just gives you a chance for it to be wrong and out-of-sync with the actual length of the array. If you want the length of the array, you should just retrieve it upon demand with:

    object.a.length
    

    If you really want it to be a property (that has a fixed value and doesn’t change when a changes), you can set it after you create the object:

    var object = {
       a:[1,2,3,2],
    };
    object.b = object.a.length;
    alert(object.b);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to create an object in python that is a collection of around
I want to create an object with a non-enumerable property(a property that does not
I want to create a Map object that can lose value only if we
I want to create a new object using this $procedure = new ${$s.'\\'.$p}; It
I have a dilemna that needs I've been thinking a while but still haven't
I want to create a class that exends Comparator. This comparator will compare two
I want to create a string and pass it by reference such that I
I have a method that will provide an array of model object. Some of
I have multiple models that I want to create generic inputs for. My first
I create an object thus: var savingArray = new Array({doctorId: something, username: something, password:

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.