Linked Questions
17 questions linked to/from Can I reference other properties during object declaration in JavaScript?
936 votes
32 answers
221k views
Self-references in object literals / initializers
Is there any way to get something like the following to work in JavaScript? var foo = { a: 5, b: 6, c: this.a + this.b // Doesn't work }; In the current form, this code obviously throws ...
1 vote
2 answers
2k views
Using one object value in another value on the same object Javascript [duplicate]
My question is somewhat simple, but IDK if I haven't made the search with the right keywords, but my problem is: When defining an object in JS/TS, how can I use one value from the obj into another key,...
0 votes
1 answer
1k views
In Javascript, can I reference a key value pair from another key value pair? [duplicate]
var fighters = ['johnjones', 'rondarousey', 'connormcgregor', 'chuckliddel', 'demetriusjohnson']; var warriors = { wrestlers: ['randysavage', 'hulkhogan', 'ultimatewarrior', 'jakethesnake', '...
0 votes
3 answers
294 views
Uncaught TypeError: in JS [duplicate]
I am getting: "Uncaught TypeError: Cannot read property '0' of undefined at newTipCalc.js:4" Can anybody tell what is wrong: var johnTipCal = { name: 'John Smith', bills: [124,48,...
1 vote
1 answer
206 views
Calling this inside nested object in array return undefined [duplicate]
I'm trying to call this inside a nested object in an array in node.js, but it's returning undefined. var foo = { dog: 'max', cat: { names: [ { grey: this.dog } ] ...
-2 votes
1 answer
275 views
Self assign properties in an object? [duplicate]
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 ...
-1 votes
1 answer
68 views
How to compute object properties in object literal based on other properties [duplicate]
I'm doing to subtract inside the json is this possible? @employees = [ { "name": "Fullname1" "vl": "Vacation Leave", "vl_days": "15.0", "sl": "Sick Leave", "sl_days": "10.0", "bday": "...
0 votes
1 answer
46 views
'this' keyword do not refer to its own object [duplicate]
let obj = { a:2, b: this.a, }; console.log(obj.a); //outputs 2 console.log(obj.b); //outputs undefined Why can't I access one property of an object from another property within the same ...
0 votes
0 answers
34 views
Use object values as new values inside same object [duplicate]
I am having the following object: obj = config { commonId: "id-goes-here", option1 : { value : "", link : "some-link/" + commonId should go here }, option2 : { ...
0 votes
1 answer
33 views
Accessing the element of object in same object [duplicate]
let mario = { x: 20, w: 20, h: 40, y: canvas.height-mario.h, speedX: 0, speedY: 10, dx: 4, dy: -7, color: "black", canJump: true, onGround: true,...
235 votes
8 answers
160k views
How can a JavaScript object refer to values in itself? [duplicate]
Lets say I have the following JavaScript: var obj = { key1 : "it ", key2 : key1 + " works!" }; alert(obj.key2); This errors with "key1 is not defined". I have tried ...
11 votes
3 answers
16k views
Value of this inside arrow function inside an Object [duplicate]
I thought I understood the relationship between this and arrow functions but the following snippet is making me question my understanding. let person = { name: 'Jim', sayName: () => { ...
8 votes
4 answers
752 views
Setting two properties equal in the declaration
I want to set two properties equal to each other in one object. Here's an example: var obj = { // I want to do something like this a: function() { ... }, b: alert, c: a }; Obviously that ...
2 votes
3 answers
2k views
How to reference an object inside itself?
How to reference an object inside itself? As you can see below I'm creating an unnamed object inside the console.log console.log({ x: 2, y: this.x * 2, }); I'm not assigning it to ...
5 votes
3 answers
241 views
How to set an object function equal to another [duplicate]
Assuming I have the following object: let obj={ childone: (value) => { return value+1; }, childtwo: (value) => { return value+3; }, childsingle: (value) =>...