Javascript IIFE , Objects [duplicate] - javascript

This question already has answers here:
How do JavaScript closures work?
(86 answers)
Closed 4 years ago.
Please give me some link to help me understand this
var obj = {
a: 1
};
(function(obj) {
obj = {
a: 2
};
})(obj);
console.log(obj.a);
logs out 1 whereas this
var obj = {
a: 1
};
(function() {
obj = {
a: 2
};
})();
console.log(obj.a);
logs out 2

It is because in example 1 you are creating a new name obj as a parameter and it's getting overridden instead of using the passed in value. In example 2, obj is being closed around and replaced.

Related

Odd syntax for setting an object's property in JavaScript [duplicate]

This question already has answers here:
What does this symbol mean in JavaScript?
(1 answer)
What do square brackets around a property name in an object literal mean?
(2 answers)
Closed 3 months ago.
What is happening in the code on line 10 ({[last]: newObj}) in the following snippet:
How is JS able to use the value of parameter last instead of using last as the property name?
let first = 'first';
let last = 'last';
function foo(first, last) {
let newObj = {
name: 'newObj'
};
let obj = {};
Object.assign(obj, {[last]: newObj});
return obj;
}
console.log(foo('bye', 'hey')); // { hey: { name: 'newObj' } }
Thanks.

How to access a deep level in an object with a string parameter? [duplicate]

This question already has answers here:
Accessing nested JavaScript objects and arrays by string path
(44 answers)
Convert a JavaScript string in dot notation into an object reference
(34 answers)
Closed 3 years ago.
I have a sample object like this:
const test1 = {
a: {
b: {
c: 2
}
}
}
When I pass a parameter like: 'a', 'a.b' or 'a.b.c'. How can access the right level of the object in an effective way?
I tried to loop through it but couldn't figure out a method to do it properly.
Any ideas? Thanks,
const test1 = {
a: {
b: {
c: 2
}
}
}
const item1 = 'a.b'
const item2 = 'a.b.c'
console.log(test1[item1]) // should be { c: 2 }
console.log(test1[item2]) // should be 2

ReferenceError: Cannot access 'obj' before initialization [duplicate]

This question already has answers here:
Self-references in object literals / initializers
(30 answers)
Closed 3 years ago.
I have this object:
const obj = {
thing: 5,
layer: {
otherThing: obj.thing - 2
}
}
error:
ReferenceError: Cannot access 'obj' before initialization
I tried to use this but it didn't work as expected.
This is not something you can do in JavaScript. But you have two possible alternatives here:
1)
const obj = {thing: 5, layer: {}};
obj.layer.otherThing = obj.thing - 2;
2) getters
const obj = {
thing: 5,
layer: {
get otherThing(){obj.thing - 2}
}
}

Destructure and retrieve the fully structured variable in one expression [duplicate]

This question already has answers here:
Destructure object parameter, but also have reference to the parameter as an object? [duplicate]
(2 answers)
Closed 3 years ago.
Is it possible to both destructure a variable and have access to the structured variable in the same call?
For example, what could I replace ??? below to get the desired output (and how else might I have to edit my code)
const foo = ({ a, b }) => {
console.log(a) // 1
console.log(b) // 2
console.log(???) // { a: 1, b: 2 }
}
const x = { a: 1, b: 2 }
foo(x)
My goal is knowledge and succinct code - I want to avoid const { a, b } = params as the first line of foo() in the case where I might need to pass the entire params object on.
If the first argument is an object whose reference you want, it's not possible - once you destructure an argument, there's no longer any way to reference to the full argument, only some of its properties.
It would be possible if the object and parts you wanted was part of a larger object, though, because then you can reference the property name alone (to get the object into a variable), and then reference it again to destructure, eg:
const foo = ({ obj: { a, b }, obj }) => {
console.log(a) // 1
console.log(b) // 2
console.log(obj) // { a: 1, b: 2 }
}
const obj = { a: 1, b: 2 }
foo({ obj })
Your original code could work to an extent if the object had a property that referenced itself, but that's pretty weird:
const foo = ({ a, b, obj }) => {
console.log(a) // 1
console.log(b) // 2
console.log(obj) // { a: 1, b: 2 }
}
const x = { a: 1, b: 2 }
x.obj = x;
foo(x)

Shorthand for creating object with same field [duplicate]

This question already has answers here:
One-liner to take some properties from object in ES 6
(12 answers)
Closed 5 years ago.
const json = {
a: 1,
b: 2,
c: "HI"
}
Is there a way to just pass in {a : 1} to function?
var someReturn = aOnly({a : json.a});
I am not sure if destructuring/constructuring of ES6 solves this problem. I can't seem to find the right syntax after reading several ES6 destructuring example.
EDIT
Nothing is wrong with the code I am just asking if there is a syntax to just extract "a" only from JSON without redundantly doing
{a : json.a }
the implementation of aOnly function is not the focus here
You can use ES6 object destructuring on object passed as parameter to pick specific property.
const json = {
a: 1,
b: 2,
c: "HI"
}
function aOnly({a}) {
return a;
}
var someReturn = aOnly(json);
console.log(someReturn)
Thank you I have figured out the right syntax. The initial mistake I was doing was trying to construct directly using { json.a }, hoping that this will automatically turn into { a : json.a} but the right way is do destruct first and construct later.
var json = {
a : "ASDF",
b : 123,
c : "Test"
};
const {a, b, c} = json; // destructing
var aObject = aOnly({a}); // constructing
var bObject = bOnly({b});
var cObject = cOnly({c});
console.log (aObject);
console.log (bObject);
console.log (cObject);
function aOnly(a) { return a; }
function bOnly(b) { return b; }
function cOnly(c) { return c; }

Categories

Resources