Self assign properties in an object? [duplicate] - javascript

This question already has answers here:
Self-references in object literals / initializers
(30 answers)
Can I reference other properties during object declaration in JavaScript? [duplicate]
(7 answers)
Closed 8 years ago.
I am trying to initialize an object and and assign a property of its own to one of the properties.
But my syntax is incorrect.i am referring to the following line:
PCMACUrl = genericURL + "/test"
i have tried
testList[0] = {
executionTimeSec:60,
genericURL:"www.gmail.com",
comments: "",
PCMACUrl = genericURL + "/test"
};

After re-reading all together I believe this is what you are looking for:
(added after init, yes, I know, but it's clean and simple :)
var data = {
'PropA': 1,
'PropB': 2,
'PropC': 3
};
data.PropD = data.PropC+5;
console.log(data); //Object {PropA: 1, PropB: 2, PropC: 3, PropD: 8}
Or, another way to look at it:
if it is possible use backend to generate the object you would like
probably you could also just get rid of same data in same object and use it differently when you call it (referencing to first object propertie and adding second on-the-go:
.../ = data.PropC+anotherData.PropA /...

Related

How to access Object's property itself while defining it? [duplicate]

This question already has answers here:
Self-references in object literals / initializers
(30 answers)
Closed 4 years ago.
I am trying to dynamically use the previous property's value for calculation of next property.
I have a function like this in Typescript:
MacroGenerator(calories) {
this.caloriedata['macroarray'] = [
{
name: 'Low Carb, High Fat',
pmacro: (Math.round(calories*220.46226218100)/100),
pcals: (4*this.caloriedata['macroarray'][0].pmacro), // THIS IS HOW I TRIED ACCESSING THE PROPERTY AND GETTING ERROR
fcals: (calories*0.3),
fmacro: (Math.round(this.caloriedata['macroarray'][0].fcals/9)/100),
ccals: (calories-this.caloriedata['macroarray'][0].pcals-this.caloriedata['macroarray'][0].fcals),
cmacro: (Math.round(this.caloriedata['macroarray'][0].ccals/4)/100),
}
]
}
I suppose the object isn't instantiated as of when I am trying to access.
Is there any way to access it?
You can use Javascript Getters
From MDN
Sometimes it is desirable to allow access to a property that returns a
dynamically computed value, or you may want to reflect the status of
an internal variable without requiring the use of explicit method
calls. In JavaScript, this can be accomplished with the use of a
getter
var obj = {
a: 4,
get b() {
return this.a * 2;
}
}
console.log(obj.b)

Javascript array value not printing [duplicate]

This question already has answers here:
Self-references in object literals / initializers
(30 answers)
Access object properties within object [duplicate]
(2 answers)
Closed 6 years ago.
How to do ElementName get value from ElementId?
var Elements = {
ElementId: '#myelement',
ElementName: Elements.ElementId
};
alert(Elements.ElementName);
You haven't created the object yet, so it's not going to be defined. You can define that property on the object once it's created like this:
var Elements = {
ElementId: '#myelement'
};
Elements['ElementName'] = Elements.ElementId;
alert(Elements.ElementName);
However I think there's a better way; what specifically are you using the name and id properties for?

JavaScript Object and Properties [duplicate]

This question already has answers here:
Self-references in object literals / initializers
(30 answers)
Closed 7 years ago.
Is it possible to reuse a property during the declaration in JavaScript?
Example : phone_min: breakpoint.small_max + 1,
Code
var breakpoint = {
small_max: 479,
phone_min: breakpoint.small_max + 1,
};
I got error :
Uncaught TypeError: Cannot read property 'small_max' of undefined
No, you cannot do that. In an object initializer, it's not possible to refer to the object that is "under construction".
No it is not possible in JavaScript. You can save small_max in variable and then use it:
var small_max = 479;
var breakpoint = {
small_max: small_max,
phone_min: small_max + 1,
};

Defining property of a variable [duplicate]

This question already has answers here:
Self-references in object literals / initializers
(30 answers)
Closed 8 years ago.
I am very new to JS.
I am trying to define properties of a variable, but the trick is that I want JS to define a new variable while defining another.
This does not work:
var robber = {
health: 10,
halfHealth: this.health/2,
};
I expect robber.halfHealth to be 5, but answer is NaN. I guess it does it because var robber is not really defined by the time attempt to calculate halfHealth is done?
If I put it an another way it works:
var robber = {
health: 10,
// halfHealth: this.health/2,
};
var robberHalfHealth = robber.health/2;
I do not want to have hundreds of variables, but want all variables related to "robber" to live {in one house}, so to say.
P.S. One of ways might be to add function which would define halfHealth and do robber.init(), but is there a more straightforward solution?
Why not use a function?
var robber = { health: 10, halfHealth: function(){return this.health/2;} }
robber.halfHealth(); // 5

Translating literal object into object's properties [duplicate]

This question already has answers here:
How to duplicate object properties in another object? [duplicate]
(16 answers)
Closed 8 years ago.
Given this object:
var myObject = {name: 'David', date: '11/13/2014'};
I want my constructor to set its object properties based on myObject:
function myClass(_object){
// set given object's properties here
}
I want to set the properties through a loop, so I wont have to set every property by hand - because the object will be dynamic.
I'm looking for a cross-browser solution.
Perhaps what you want is this:
function MyClass(_object) {
for (var p in _object) {
if (_object.hasOwnProperty(p)) {
this[p] = _object[p];
}
}
}
Don't forget to use new:
var obj = new MyClass(myObject);
Fiddle

Categories