Accessing values of object using "this" [duplicate] - javascript

This question already has answers here:
Access object properties within object [duplicate]
(2 answers)
Closed 7 years ago.
I have this code with me,
var x = {
a: 10,
b: 20,
c: (this.a + this.b)
};
where this.a and this.b is coming as undefined. So as a result of adding the both and printing it displays NaN. Also I tried with (x.a + x.b). The results were the same.
Can anyone tell how do I access a JSON object's value internally using this? May be other way?

Better way to using the same object value in JSon using "this"
var x = {
a: 10,
b: 20,
c: function(){return (this.a + this.b)}
};
console.log(x.c())

Related

JavaScript Object Property Instantiation [duplicate]

This question already has answers here:
Javascript object initialization and evaluation order [duplicate]
(2 answers)
Are Javascript Object Properties assigned in order?
(5 answers)
Closed 2 years ago.
If you have an object where the properties are created by calling a function or a constructor, is the order of execution of these guaranteed?
Example:
const testObject = {
foo: new Date().valueOf(),
bar: new Date().valueOf()
};
console.log(testObject.foo > testObject.bar);
Is it ever possible that foo will be greater than bar?
You can try it for yourself. In the latest firefox and chrome it appears to be evaluated as written:
let i = 0;
const counter = () => i++;
const testObj = {
a: counter(),
b: counter(),
c: counter(),
d: counter(),
e: counter()
}
console.log(testObj)

Overriding function named params in Javascript? [duplicate]

This question already has answers here:
Where can I get info on the object parameter syntax for JavaScript functions?
(1 answer)
ES6 Object Destructuring Default Parameters
(1 answer)
Closed 2 years ago.
function lookupRecord2({value1 = "blue", value2 = 7}) {
console.log('theValues');
console.log(value1);
console.log(value2);
}
lookupRecord2( { value1: 'pink', value2: 9 } );
I've read that Javascript doesn't allow for named parameters so I'm confused how the above code works. What is happening here that's allowing me to override the params?
I think what's confusing is that the params are assigned with "=" whereas the object I'm passing uses ":" ...
ADDED:
I know this is a form of destructuring, but I still can't make sense of it b/c it's in the function declaration.
function des({ a = 1, b = 2 } = {}) {
console.log(a);
console.log(b);
}
des();
This is a destructing syntax where we're able to pass default values.
It works everywhere, not just in function declarations.
{ a = 1, b = 2 } = { b:3 }
// assigns 1 to "a" and because "b" already exists, "b" is 3
MORE INFO HERE: can someone explain this seemingly weird assignment `{key=value} = argument`

Accessing parent object in javascript [duplicate]

This question already has answers here:
Access parent's parent from javascript object
(12 answers)
Closed 8 years ago.
I have the following code in javascript :
var x = {
a:1,
b : {
c : 2,
d : //i want value of x.a in here
}
}
I have read that accessing parent property like this is not at all possible. Is there any workaround for this?
No, there isn't. Nested objects don't have access to their hosts as in any other language as well.
You have to set the explicitly by yourself.
No, it's not possible, but your case can be written like:
var x = new function(){
this.a = 1;
this.b = {
c: 2,
d: this.a
}
};
A javascript object doesn't have a parent because you can have sharing. For example:
var a = { x: 42 };
var b = { y: a };
var c = { z: a };
here the object a is "shared" as sub-object between b and c, so what should be the "parent" or a? You can see the sharing because after executing b.y.x = 99, also c.z.x will show be 99. A single javascript object may be reachable using different paths.
DOM objects on the other hand have a parent because the DOM is a tree structure and it makes sense to talk about "the" parent of a node.
If you add a DOM node as a child of another and the node is already part of the DOM it will be removed from where it is and it will be placed in the new position.
The parent reference does not exist in javascript. What you can do is build an helper recursive function that will parse all your object and add a reference to the parent. This will build a more complex object.
Here is a video which shows how to : http://blog.wax-o.com/2014/01/how-to-find-deep-and-get-parent-in-javascript-nested-objects-with-recursive-functions-and-the-reference-concept-level-beginner/
try this:
var x = {
a: {
c: 1
},
b: {
c: 2
}
}
x['b']['d'] = x['a'];
should work ;)

Getting an object with a variable. -javascript- [duplicate]

This question already has answers here:
How to use variables in dot notation like square bracket notation
(6 answers)
Closed 8 years ago.
var i = "test";
var test1 = {
test: 3,
b: 3
};
console.log(test1.i);
Sorry if it's a simple answer, I am still learning.
var i is looping to something different every few seconds and var i will always be something on test1.
If you are trying to retrieve a property of an object you can use the . notation, or the [] notation
var test1 = {
test: 3,
b: 3
};
Using . notation
test1.test; // -> returns 3
Using [] notation
var propertyName = 'test'
test1[propertyName]; // -> returns 3
Do it this way:
var test1 = {
test : 3
};
console.log(test1[i])

Access attributes inside of object while in object in JavaScript [duplicate]

This question already has answers here:
Self-references in object literals / initializers
(30 answers)
Closed 8 years ago.
I'm having a bit of an inception issue right now. I'm trying to reference an attribute of an object while creating another attribute of the object.
For example:
var x = {
a1 : "a",
b2 : this.a1,
c : "c"
}
alert(x.a1); // Returns properly
alert(x.b2); // Returns undefined.
If I try to make b2 reference x.b2, it doesn't work either. Can anyone point me in the right direction?
All in all, I'm trying to decide the value of b2 based on the value of a1 without having to take another step out of the object.
Here's a fiddle -- http://jsfiddle.net/fpG9h/
You are most definitely in need of getters and setters. You can define them like this
var obj = {
a1: "a",
get b2() {
return this.a1;
},
set b2(value) {
this.a1 = value;
},
c: "c"
}
console.log(obj.b2); // a
obj.b2 = "bbb";
console.log(obj.b2); // bbb
console.log(obj.a1); // bbb

Categories