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
  • If it is self-referencing, then it is the very same object. You cannot have one level have a property and not the other. (a) I don't think self-referencing is the right approach here. (b) Inheritance is something else. Commented Aug 19, 2011 at 23:41

2 Answers 2

4

"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
Sign up to request clarification or add additional context in comments.

5 Comments

You might be able to if you put it in a box ;) (But then you cannot access the property. It might be there or not... you will never know).
How you reach to an approach like what I described?
@Mohsen: It depends ultimately on how you want to use it. Are you referring to nested objects where you access a different object from the property of another? Or do you want a child object to inherit all the properties of a parent so you can reference the parent's properties as though they were part of the child? What's the end goal with your code?
I want to simulate something like "class". I think second scenario is mine
@Moshen: Yes, you don't really have classes in JavaScript, but you do have the prototype object chain. The two examples I added will generally give the same result, but the second one you'll be able to run in more browsers if that's your target environment. There are ways to emulate the first one in older browsers too.
0

You can't have different values at different level. Since obj.obj is obj, meaning, obj.obj holds a reference to obj, rather than a copy, this means any change to obj will change any reference to it.

Imagine an object, which we will call foo. bar and obj both holds reference to foo. Any change to the properties of foo, bar, or obj will change the value of the others. Example:

var foo = {};
var bar = foo;
var obj = foo;
bar.lol = 'mew';
bar.lol == foo.lol == obj.lol

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.