JavaScript Object and Properties [duplicate] - javascript

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,
};

Related

how to use 'this" inside a singleton object in JavaScript properly? [duplicate]

This question already has answers here:
Self-references in object literals / initializers
(30 answers)
Closed 1 year ago.
var a={b:44,c:this.b+1};
I want c to be b+1 or 45 in this case. but when I try this, I get Cannot read property 'b' of undefined error.
In JavaScript, when creating an object, the execution context doesn't change. Hence, the this value is picked up from the top-level in your case. Or to articulate in a different way, you can't access the properties of the object if it's not finished initializing yet.
The actual solution to your problem might be:
var a = { b: 44 };
a.c = a.b + 1;

Call variable names (w/ number) using for loop [duplicate]

This question already has answers here:
Accessing an object property with a dynamically-computed name
(19 answers)
Closed 4 years ago.
I'm trying to get values from req.body.answerX using for loop instead coding each of them.
I've stored values e.g. into "answer1, answer2" etc.
This was my attempt:
for( var i = 1; i <= 10; i++){
console.log(req.body.answer[i]);
}
That gives me following error:
TypeError: Cannot read property '1' of undefined
How can I achieve this?
Update from
console.log(req.body.answer[i]);
to
console.log(req.body["answer" + i]);
For reference, Property Accessor

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?

How to refer a property in a value of another property in the same Javascript Object? [duplicate]

This question already has answers here:
Self-references in object literals / initializers
(30 answers)
Closed 7 years ago.
In a Gulp.js file (but it can be any Javascript file) I set the paths in a Javascript object:
var paths = {
devDir : 'builds/development/',
devDirGlobs : this.devDir+'*.html'
}
Now, as you can see, I'm trying to recall the property "devDir" in the value of the property "devDirGlobs", as "this.devDir".
It doesn't work, but also it doesn't give to me any error?
Any hint?
Thanks in advance for your help!
It's not possible to access the properties of the object before the object has been declared.
However, you could construct the object with dot syntax rather than as a literal.
var paths = {}
paths.devDir = 'builds/development/';
paths.devDirGlobs = paths.devDir + '*.html';
Or move common values into a shared config object.
var config = {
dir: 'builds/development'
};
var paths = {
devDir : config.dir,
devDirGlobs : config.dir + '*.html'
};

Self assign properties in an object? [duplicate]

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 /...

Categories