JS object accessing private variable as an object [duplicate] - javascript

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Self-references in object literal declarations
Given this object:
var OBJ = (function(){
var dom = {
prop1 : 'something',
prop2 : 'something',
prop3 : prop1
}
return dom.prop3;
})();
How would i go to achieve the prop3 reference (ideally without creating a method) ? i tried:
this.prop1, dom.prop1, this.dom.prop1

You can't access the properties of an object before you have finished creating it. Create the object, then assign additional values.
var dom = {
prop1 : 'something',
prop2 : 'something'
};
dom.prop3 = dom.prop1;

Related

How to use an object key in another object's path? [duplicate]

This question already has answers here:
Accessing an object property with a dynamically-computed name
(19 answers)
Closed 5 years ago.
I have the following objects:
var obj1 {
key1: "path"
};
var obj2 {
path: "done"
};
I want to get "done" (obj2.path) in the scope of obj1 key1.
So the key "path" shall not be reached by obj2.path, but by obj1.key1.
Something like: obj2.(obj1.key1)?
i hope you can understand, sorry for my english :)
You can try using the [] notation to access property, which derived from obj1 key.
var obj1= {
key1: "path"
};
var obj2= {
path: "done"
};
console.log(obj2[obj1.key1]);

Create a JSON field according to the value of a variable [duplicate]

This question already has answers here:
Is it possible to add dynamically named properties to JavaScript object?
(20 answers)
Closed 6 years ago.
Supposing we have JSON data who came as :
msg = {
fieldName: fieldA
};
msg = {
fieldName: fieldB
};
I can receive multiple of this message, but everytime i have a different field, i want it to add in the 'data' json object, as i can access by using this :
data.fieldA
data.fieldB
By which way can i perform this ?
To access a property of an object when the name of the property is something your code figures out at runtime:
var propertyName = someCode();
var propertyValue = someObject[propertyName];
To create an object with an object initializer and a computed property name, you can do this in modern (ES2015) JavaScript:
var propertyName = someCode();
var someObject = {
[propertyName]: someValue
};
Prior to ES2015, you would do this:
var propertyName = someCode();
var someObject = {};
someObject[propertyName] = someValue;

How to add a new property in object in javascript [duplicate]

This question already has answers here:
Is it possible to add dynamically named properties to JavaScript object?
(20 answers)
Closed 6 years ago.
I have one obj like
var obj = {
name : "Jenny",
roll : "1000"
}
Now I want to add a new property in obj called grade.
after that my object looks like
var obj = {
name : "Jenny",
roll : "1000",
grade :"7"
}
I am not getting any method from obj obj[op]=value. How to achive that.
It is an object so you can not do obj[op] = value
Instead you can do obj.op = value
In this case, obj.grade = 7 will serve the purpose.
You can do this.
Object.defineProperty(obj, 'grade', {
value: '7'
});

Access internal Property Name to Compute New Property in ES6 [duplicate]

This question already has answers here:
Self-references in object literals / initializers
(30 answers)
Closed 7 years ago.
I have object. I want to add properties but I want to compute the suffix for all properties in my object during definition.
var myObject = {
foo: "bar",
[ "prop_" + "Access foo property: foo" ]: 42
};
Below is my expected output:
{
foo: "bar",
prop_bar: 42
}
It's not the case that i'm unable to achieve it. i can able to achieve by the below snippet and its working fine but i want this to be done during declaration.
let myObject = {
foo: "bar"
};
myObject[ "prop_" + myObject['foo'] ] = 'hello'
Note to Reviewers: I have already reviewed the below questions.
Is there a shorthand for this in ES6/ES7?
ES6 Object Literal Property Value Shorthand
How to use ES6 computed property names in node / iojs?
ES6 Computed (dynamic) property names
I feel that there will be better solution than above approach, below are my questions.
What is best approach for this scenario?
How many ways we can achieve this ?
Its not possible during declaration ?
You can create factory function to do this, e.g.:
const create = v => ({
foo: v,
["prop_" + v]: 42
});
let myObject = create("bar");
or inline:
let myObject = (v => ({
foo: v,
["prop_" + v]: 42
}))("bar");

Reference own properties on initialization [duplicate]

This question already has answers here:
How do I reference the same Object's properties during its creation? [duplicate]
(5 answers)
Closed 9 years ago.
Can one reference own properties on initialization of object?
I have an object that should be unique and only exist as one so I
have no initializer etc.
Using something like this:
var Myobj = {
property1 : aaa,
property2 : bbb,
property3 : {a: self.property1 }
/* ^-- is this somehow possible without using a function?
Or a better way to solve / structure this. .init()
best option? */
};
>> Fiddle <<
The real object, (in the real code), has an add function that takes options on
what function to use etc. It is sort of a wrapper for "addEventListener" where
the point is to be able to remove listener - which require non anonymous function
reference.
Function to use within Myobj is specified by string, or numeric key in options to
Myobj.add().
Object literals remain undefined until the closing }, so you'd either have to make property3 a function or make the whole object a function.
var Myobj = {
property1 : 'aaa',
property3 : function (){ return { a : this.property1 } }
};
or
var MyObj = function(){
this.property1 = 'aaa',
this.property3 = { a: this.property1 }
}
var MyObjInstance = new MyObj();
This has been asked a lot of times: Access properties while declaring object?
It works if you use a placeholder array:
var prop = new Array();
var Myobj = {
property1 : 'aaa',
property2 : 'bbb',
property3 : {a: prop}
};
prop.push(Myobj.property1);
window.alert(Myobj.property3.a);
http://jsfiddle.net/y2XwC/1/

Categories

Resources