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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T00:27:20+00:00 2026-06-08T00:27:20+00:00

So I am new to object definition in Javascript, and am trying to write

  • 0

So I am new to object definition in Javascript, and am trying to write a program that revolves objects as a practice. My problem is that when I am trying to define the object, some of the object properties are dependent on other parts of the object. I am not sure if this is even permitted, because in all my searching I have not been able to find any examples of it.

My question is basically this: can I use previously defined properties of an object to define that object. The most basic example of this would be something like this:

var alfred = {
    dogs: 1,
    cats:this.dogs+1,
}

Is this permitted? if so is this the right syntax? The reason I need to use a “this.” is because I am pushing newly created objects to a array of objects.The code of mine that is not working is below:

obj.push({
    canvas:document.getElementById(canvasName),

    canvasName:"canvas"+objNum,
    image: img,
    width:objWidth,
    height:objHeight,
    centerX:posX,
    centerY:posY,
    speed:speeds,
    hypSquare:Math.sqrt((this.width*this.width)+(this.height*this.height)),
    angleInSquare:Math.atan(this.height/this.width),
    angle:startAngle,
    angleTotal:this.angle+this.angleInSquare,
    offX:(this.hypSquare* Math.cos(this.anglesTotal))/2,
    offY:(this.hypSquare* Math.sin(this.anglesTotal))/2,
    centeredX:this.centerX-this.offX,
    centeredY:this.centerY-this.offY,
})

when I call a

console.log(obj[objNum].hypSquare);

(where objNum is just the index of the object in the array) I will get NaN even though if I call

 console.log(obj[objNum].width);

I will get the value of objWidth. Is there just a syntactical issue, or is my understanding of objects fundamentally flawed…

Thank you in advance for your time!

Isaac

  • 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-08T00:27:22+00:00Added an answer on June 8, 2026 at 12:27 am

    No, you can’t do that. You have to close the object initializer and then add the other property, e.g.:

    var alfred = {
        dogs: 1
    };
    alfred.cats = alfred.dogs + 1;
    

    So for your obj.push call, you’ll have to use a temporary variable (like alfred above), you can’t just use an inline object initializer.

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

Sidebar

Related Questions

Whats wrong in below JSON Object definition I am trying to create a new
I created a new object() in Javascript, put some values into it and passed
I am a bit new to javascript and was trying to understand how the
I've been doing some reading about two (relatively) new concepts in the Javascript language
I've been trying to create a wrapper function that takes all the function properties
I'm trying to output a list of properties from a Mongoose object, but I
Let's suppose I want to create a javascript class/object/function which have a method that
I have an object, MySession , that has a hashtable for storing arbitrary properties
I am trying to get my head around Object Oriented Programming in Javascript and
object[] objs = new object[]{one,two,three}; Are the strings stored in the array as references

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.