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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T15:31:42+00:00 2026-05-25T15:31:42+00:00

I want to reference a nested property in an object literal from within another

  • 0

I want to reference a nested property in an object literal from within another property in that same object literal.

Consider the following contrived example:

var obj = {
   product1: {
      price: 80,
      price_was: 100,
      discount: function(){

        return 100 - (100 * (price/price_was));

        //I don't want to use:  
        //100 - (100 * (this.product1.price/this.product1.price_was))
        //because the name of the parent ('product1' in this case) isn't known 
        //a-priori.

      }
   }
} 

The above is obviously incorrect, but how to get to ‘price’ and ‘price_was’ from within ‘discount’?

I’ve looked at the following question, which is close, but in that question the needed property is a direct child of ‘this’, which in the above example isn’t the case.
reference variable in object literal?

Any way to do this?

  • 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-25T15:31:43+00:00Added an answer on May 25, 2026 at 3:31 pm

    “…in that question the needed property is a direct child of ‘this’, which in the above example isn’t the case”

    Actually, it probably is if you’re calling .discount() from the productN object.

    So you wouldn’t use this.product1.price, because if you’re calling discount from productN, then this will be a reference to productN.

    Just do this:

    this.price;
    this.price_was;
    

    …so it would look like:

    var obj = {
       product1: {
          price: 80,
          price_was: 100,
          discount: function(){
    
            return 100 - (100 * (this.price/this.price_was));
    
          }
       }
    };
    

    Again, this assumes you’re calling the function from the productN object. If not, it would be helpful if you would show how discount is being called.

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

Sidebar

Related Questions

I want to hyperlink to a page within the Word 2007 Object Model Reference
So My problem is: I want to create nested array from string as reference.
I want to hold reference to object so it doesn't get deleted in bind
I have a user control within a user control. I want to reference the
Explanation: Let's say I have an object graph that's nested several levels deep and
I want to reference a COM DLL in a .NET project, but I also
In this particular code sample I want to reference the second overloaded method (int
I am using the pimpl idiom and want to reference one of the methods
Looking for good project methodologies.Anyone got some good reference links..want to share.
I want to get a reference to the Gmail Inbox button in Javascript? I

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.