JavaScript Object Property Instantiation [duplicate] - javascript

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)

Related

how to use call function in Javascript? [duplicate]

This question already has answers here:
When should I use arrow functions in ECMAScript 6?
(9 answers)
Can you bind 'this' in an arrow function?
(14 answers)
Are 'Arrow Functions' and 'Functions' equivalent / interchangeable?
(4 answers)
Closed 5 years ago.
I'm being a bit puzzled when I try to convert a simple ES5 code to ES6.
Let's say I have this block of code:
var obj = {num: 2}
var addToThis = function (a, b, c) {
return this.num + a + b + c
}
// call
console.log(addToThis.call(obj, 1, 2, 3))
// apply
const arr = [1, 2, 3]
console.log(addToThis.apply(obj, arr))
// bind
const bound = addToThis.bind(obj)
console.log(bound(1, 2, 3))
Everything above runs smoothly and as expected.
But as soon as I start using ES6 features such as const and arrow function, like this:
const obj = {num: 2}
const addToThis = (a, b, c) => {
return this.num + a + b + c
}
It doesn't work anymore and throws an error: Cannot read property 'num' of undefined.
Can someone please explain why this doesn't work anymore?
Lambda functions (arrow functions) doesn't create new functional context and use context of a calling function.
So "this" refers to a parent context. If there's no 'num' variable than it's undefined.
Usually it's really convenient because most of the time you use one context instead of creating a new one in every function you create. In my opinion call/apply/bind is completely confusing and lambda functions makes it unnecessary.

Why does a const constant assign an object, and a constant can modify the value of the object? [duplicate]

This question already has answers here:
Why can I change a constant object in javascript
(12 answers)
Closed 4 years ago.
enter image description here
Why myVariable can be modified?
const obj = {
a: 'a'
}
const myVariable = obj;
try{
myVariable = { a: 'c' } //The const declaration creates a read-only reference to a value
}catch(e){
console.log(e);
}
myVariable.a = 'b';
console.log(myVariable); //{a: "b"}
This happens because your constant is actually storing a reference to the object. When you're adding to object you're not re-assigning or re-declaring the constant,you're just adding to the "list" that the constant points to.Refer more here : Why can I change value of a constant in javascript

Weird javascript code convention with "const" [duplicate]

This question already has answers here:
beginner's: const definition in Redux confusing
(1 answer)
What does curly brackets in the `var { ... } = ...` statements do?
(4 answers)
Closed 5 years ago.
What is this code convention in javascript?
const { navigate } = //whatever
As in, what sense does it make. I saw it in RNs React navigation
https://reactnavigation.org/docs/intro/
It's named destructuring. When you have an object and you want to take only a property of that object, you can get only it by using that convention.
let fullName = {
first: 'John',
last: 'Smith'
}
const { first } = fullName;
You can take a look here for more info's
http://wesbos.com/destructuring-renaming/
It's called destructuring
Example:
var myObject = {a: "what", b: "ever"};
const {a} = myObject;
console.log(a); // will give "what"

No colon after property name in object declaration, is it valid? [duplicate]

This question already has answers here:
How does this object method definition work without the "function" keyword?
(2 answers)
Closed 6 years ago.
I entered this expression in the Firefox and Chrome Dev Console and I wonder why it is valid JavaScript:
var x = { a (b) {} };
console.log(x);
x is then set to an object with the property "a" containing a function called "a" with an argument identifier "b".
How is this valid JavaScript syntax? The colon is missing after "a" and I do not understand the function definition.
This is ES6 / ES2015 syntactic sugar (Property shorthand).
With ES6:
const obj = {
a(b) {
// Shorthand method
// `this` context is `obj`
},
c
};
is equal to
var obj = {
a: function a(b) {
},
c: c
};
In JavaScript, when you write:
var x = { a (b) {} };
It will consider it as:
var x = {
a: function (b) {
}
}
For example, you can check this and it will clear your doubt:
var x = { a (b) { console.info('function called') } };
x.a();
This will call the function which is assigned to property a of object x.

Accessing values of object using "this" [duplicate]

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())

Categories