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

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}
}
}

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.

Wsy this arrow function doesn't work with 'arguments.length'? [duplicate]

This question already has answers here:
Are 'Arrow Functions' and 'Functions' equivalent / interchangeable?
(4 answers)
Why do arrow functions not have the arguments array? [duplicate]
(1 answer)
Closed 2 years ago.
I'm wondering why the following arrow function, named 'countArg2' doesn't work.
Is there anybody can explain what's wrong please?
This works
function countArg1() {
return arguments.length;
}
countArg1(1, 2, 3); //3
This doesn't work..
const countArg2 = () => arguments.length;
countArg2(1, 2, 3);
// VM6745:1 Uncaught ReferenceError: arguments is not defined
Thank you in advance.
You have to parse the arguments to the arrow function like this
const countArg2 = (...arguments) => arguments.length;
console.log(countArg2(1, 2, 3));
// VM6745:1 Uncaught ReferenceError: arguments is not defined
// at mArgs (<anonymous>:1:29)
// at <anonymous>:2:1

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

Javascript IIFE , Objects [duplicate]

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.

Access internal Property Name to Compute New Property in ES6 [duplicate]

This question already has answers here:
Self-references in object literals / initializers
(30 answers)
Closed 7 years ago.
I have object. I want to add properties but I want to compute the suffix for all properties in my object during definition.
var myObject = {
foo: "bar",
[ "prop_" + "Access foo property: foo" ]: 42
};
Below is my expected output:
{
foo: "bar",
prop_bar: 42
}
It's not the case that i'm unable to achieve it. i can able to achieve by the below snippet and its working fine but i want this to be done during declaration.
let myObject = {
foo: "bar"
};
myObject[ "prop_" + myObject['foo'] ] = 'hello'
Note to Reviewers: I have already reviewed the below questions.
Is there a shorthand for this in ES6/ES7?
ES6 Object Literal Property Value Shorthand
How to use ES6 computed property names in node / iojs?
ES6 Computed (dynamic) property names
I feel that there will be better solution than above approach, below are my questions.
What is best approach for this scenario?
How many ways we can achieve this ?
Its not possible during declaration ?
You can create factory function to do this, e.g.:
const create = v => ({
foo: v,
["prop_" + v]: 42
});
let myObject = create("bar");
or inline:
let myObject = (v => ({
foo: v,
["prop_" + v]: 42
}))("bar");

Categories

Resources