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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T23:45:35+00:00 2026-05-24T23:45:35+00:00

I just learned about self-referencing objects in this question . This is awesome that

  • 0

I just learned about self-referencing objects in this question. This is awesome that people here at StackOverflow know every dark corner of JavaScript.

Today I wanted to use my knowledge and use a self-referencing object in my code but I couldn’t make it.
I’m trying to make a self referencing object that have different values in different levels. I want every level of my object have the main properties that I’m defining in level one and level two and below have all properties of level two and so on…
Let’s see what I’m trying to do:

obj is an object that is self referencing, it means obj.obj refer to obj and so on…
I want to have a property in obj.obj but not in obj, like obj.obj.newProp = "myString" and then every level below the level two should have the newProp.
This is the code I wrote that I’m sure is not current!

var obj = {},
    obj.obj = obj,
    obj.obj.newProp = "myString";

But when I look at obj it contains "myString". How can I prevent obj refering to obj.obj?

  • 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-24T23:45:37+00:00Added an answer on May 24, 2026 at 11:45 pm

    “I want to have a property in obj.obj but not in obj”

    But they’re the same object. You can’t have and not have a specific property on the same object at the same time.

     // obj is a reference to__
     //                       |
    var obj = {}; //<---------|
    //         ^
    //         |----------------------------------------------
    //                                       |                |
    obj.obj = obj; // "obj" is a property on | that refers to |
    

    EDIT:

    If you’re talking about inheritance, then you need to set one object as the prototype object of another. There are a couple of different approaches.

    If you’re only supporting modern JavaScript environments, you can use Object.create().

    var obj1 = { prop1:'value 1' };
    
    var obj2 = Object.create( obj1 );
    
    obj2.prop2 = 'value 2';
    
    obj1.prop1; // value 1
    obj1.prop2; // undefined
    
    // obj2 inherits the properties of obj1 through the prototype chain
    obj2.prop1; // value 1
    obj2.prop2; // value 2
    

    If you’re supporting older browsers, then you can use a constructor function, and set the prototype object of the constructor to obj1, and all objects created from the constructor will have obj1 in its prototype chain.

    function MyConstructor() {}
    
    var obj1 = { prop1:'value 1' };
    
    MyConstructor.prototype = obj1;
    
    var obj2 = new MyConstructor();
    
    obj2.prop2 = 'value 2';
    
    obj1.prop1; // value 1
    obj1.prop2; // undefined
    
    // obj2 inherits the properties of obj1 through the prototype chain
    obj2.prop1; // value 1
    obj2.prop2; // value 2
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I just posted this question and learned about <see cref=> , however when i
I just learned about the Template Method pattern in this answer to a question
I just learned about ngrep , a cool program that lets you easily sniff
I've got an application that just shipped. Since I wrote it, I've learned about
I just learned about XLST on stackoverflow today (I love how in computers you
I just learned about .delegate today, and I know I can be used for
I just learned about this fine looking syntax Collections.<String>emptyList() to get an empty List
I just learned, here , about nxml-mode , which, according to the README, is
Ive just learned java and asked a question about my one line calculator it
Reading to this question i've just learned the existence of the blackhole table trick:

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.