Defining property of a variable [duplicate] - javascript

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

Related

Where can I place prototype property as per my below code? [duplicate]

This question already has answers here:
Assigning prototype methods *inside* the constructor function - why not?
(6 answers)
Closed 5 years ago.
I have writtern this two different ways and both giving me same result , so which I can use when ?
FIRST EXAMPLE
var BaseCls = function() {
BaseCls.prototype.name = "John";
};
var JustCls = new BaseCls();
console.log(JustCls.name); // This is giving result John
SECOND EXAMPLE
var BaseCls = function() {};
BaseCls.prototype.name = "John";
var JustCls = new BaseCls();
console.log(JustCls.name); // This is also giving result John
Both giving me same result so I just want to know is there any other criteria which lead to write this property / method with prototype inside / outside main function ?
Thanks for consideration
You should change prototype only outside the constructor.
Otherwise you change it every time you create an instance.

Is there a need for Setting an object to null after use in Javascript? [duplicate]

This question already has answers here:
Is it good practice to set variables to null when they are no longer needed?
(4 answers)
Closed 6 years ago.
I was wondering if there is a need for cleaning my objects following usage in JavaScript or there is some GarbageCollector that does that for me?
for example if this is my code:
function foo()
{
var test = new testClass();
console.log(test.getMessage());
//the question is if there a need for the next line or not:
test = null;
}
var testClass = function() {
this.getMessage = function () {return "My MSG" };
}
In JavaScript there is no need not need to care about garbage collecting process since it will be handled for you.

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

Javascript access object properties under same hierarchy [duplicate]

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.

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