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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T14:52:51+00:00 2026-05-23T14:52:51+00:00

When using object constructors, properties can be directly assigned to the value of previously

  • 0

When using object constructors, properties can be directly assigned to the value of previously defined properties:

var foo = new (function() {
   this.bar = 5;
   this.baz = this.bar;
})();
alert(foo.baz) // 5

I would like to refer to a previously defined property within an OBJECT LITERAL:

var foo = {
   bar : 5,
   baz : bar
}
alert (foo.baz) // I want 5, but evaluates to undefined

I know that I could do this:

var foo = {
   bar : 5,
   baz : function() {
      alert(this.bar); // 5
   }

But I want to assign baz directly to a value rather than a function. Any ideas?

  • 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-23T14:52:52+00:00Added an answer on May 23, 2026 at 2:52 pm

    You can also just build a literal by parts:

    var foo = {bar:5};
    foo.baz = foo.bar;
    

    If you need to fit this inside an expression (instead of through multiple statements) you can try abusing the comma operator or you can make a helper function:

    (Warning: untested code)

    function make_fancy_object(base_object, copies_to_make){
         var i, copy_from, copy_to_list;
         for(copy_from in copies_to_make){
             if(copies_to_make.hasOwnProperty(copy_from)){
                 copy_to_list = copies_to_make[copy_from];
                 for(var i=0; i<copy_to_list.length; i++){
                     base_object[copy_to_list[i]] = base_object[copy_from];
                 }
              }
          }
    }
    
    var foo = make_fancy_object(
        {bar: 5},
        {bar: ["baz", "biv"]}
    );
    
    //foo.baz and foo.biv should be 5 now as well.
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using C# 3.0, we can initialize objects without their constructors for syntactical reasons. Such
I have a collection of PHP classes that can be instantiated traditionally using constructors.
If I define a struct in C# using automatic properties like this: public struct
I have to create an object X using properties of object Y (both are
Does creating an object using reflection rather than calling the class constructor result in
The Java doc describe that the constructor Date(long date) constructs a Date object using
In a project our team is using object lists to perform mass operations on
I am trying to access member variables of a class without using object. please
What is the difference between the following two snippets of code: using (Object o
I am building a web application and have been told that using 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.