Javascript access object properties under same hierarchy [duplicate] - javascript

This question already has answers here:
Self-references in object literals / initializers
(30 answers)
Closed 8 years ago.
Hi i am newb to javascript when i was trying write a code i encounter this problem
var k = {
sam: {
b: k.bar.x,
},
bar: {
x: "Hi",
},
};
I dono how to access that bar.x property. I tried using getter and setters.
I know it can be accessed if i use b:this.k.bar.x . But this bad way to access it.
Please clarify my doubt. Is my understanding is wrong or my code is wrong.
Thank you

Maybe this will help, taken straight from the MDN website.
var myHonda = {color: "red", wheels: 4, engine: {cylinders: 4, size: 2.2}};
MDN Objects ref
I can't post pictures yet, but after writing that javascript, I opened my console and typed: myHonda.engine.cylinders and it returned 4. So that is how it works, to clarify.

Related

What is the purpose of Object.create() in this piece of code? [duplicate]

This question already has answers here:
Benefits of using `Object.create` for inheritance
(4 answers)
What is the reason to use the 'new' keyword at Derived.prototype = new Base
(6 answers)
Correct javascript inheritance
(2 answers)
What is the correct prototype affectation in javascript inheritance?
(2 answers)
Closed 6 months ago.
This post was edited and submitted for review 6 months ago and failed to reopen the post:
Original close reason(s) were not resolved
EDIT:
I understand the purpose of Object.create() in general. But i need to know why it is used in the below code as it makes no difference at all.
I found the below code in this article. I can understand that fruits.call(this); will essentially inherit the properties from fruits to any instance of apple that gets created. Then why do we need to do apple.prototype = Object.create(fruits.prototype); Can anyone explain the purpose of this line? I tried executing the code without the that line and it still runs with no issues.
function fruits() {
this.name = 'fruit 1';
}
function apple() {
fruits.call(this);
}
apple.prototype = Object.create(fruits.prototype);
const app = new apple();
console.log(app.name);
// Output: fruit 1

Get name of the first child in a object [duplicate]

This question already has answers here:
How to access the first property of a Javascript object?
(23 answers)
Closed 5 years ago.
I have following Object:
bots: {
bot_1: {
[...]
},
bot_2: {
[...]
},
bot_3: {
[...]
}
...
},
How could I get the name of the first child? In this case 'bot_1'. Thanks!
EDIT: I should have searched a bit more since it is a pretty basic question. I tried searching for the answer but I couldn't find it because I didn't know that it is called "property". Anyways, thanks for your help
You can do this
bots[Object.keys(bots)[0]];
to get the first key of your object. However, you may not get bot_1 always since the keys are unordered

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

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

About js objects [duplicate]

This question already has answers here:
Constructors in JavaScript objects
(19 answers)
Closed 8 years ago.
my English is not so good, but i try to explain my self clear.
I just started learn JS objects and stumbled upon the problem that i can't understand.
I got a simple object like
var cars = {
doors: 4,
wheels: 4
}
and when i trying to create an object like this:
var Opel = new car()
I got an error Uncaught TypeError: object is not a function
And when i do it like this :
Opel = Object.create(cars)
all going fine.
And when i writing a an Object like this :
function cars() {}
a method to declare the object with new, work correctly.
I can't understand what the difference between thous two type of writing the objects.
Thanks for advice.
You didn't understand prototyping correct.
To define a class you create a simple function, like:
function Car(){
this.doors = 4; //For instance, not really necessary
}
You can set properties on this in the function.
Then you define a prototype, every object of class "Car" will have all this properties (and "methods"):
Car.prototype = {
doors: 4, //we don't need to set this again if we already did in the constructor, but I'll leave if it anyway
wheels: 4
}
Please see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Introduction_to_Object-Oriented_JavaScript for more.

Categories