“this” inside an Object pointing to an empty Object [duplicate] - javascript

This question already has answers here:
Self-references in object literals / initializers
(30 answers)
Closed 5 years ago.
So I have this JavaScript code:
var obj={
a: “Chris”,
b: this
};console.log(obj.b); //{}
The console.log return an empty object rather than the object with variable a and b.
Anyone know why?
Thank you very much!

If this code is in the global scope, which I assume it is, this points to the global object (whatever it is). If you want it to point to the object itself, you cannot do it in the literal, you need to assign it later (after you create the object itself).

Related

how to use 'this" inside a singleton object in JavaScript properly? [duplicate]

This question already has answers here:
Self-references in object literals / initializers
(30 answers)
Closed 1 year ago.
var a={b:44,c:this.b+1};
I want c to be b+1 or 45 in this case. but when I try this, I get Cannot read property 'b' of undefined error.
In JavaScript, when creating an object, the execution context doesn't change. Hence, the this value is picked up from the top-level in your case. Or to articulate in a different way, you can't access the properties of the object if it's not finished initializing yet.
The actual solution to your problem might be:
var a = { b: 44 };
a.c = a.b + 1;

Define a function with a name in the string [duplicate]

This question already has answers here:
Dynamic function name in javascript?
(24 answers)
Closed 2 years ago.
I need to define a function with a name that I have in the string.
Not a big deal, right?
window[fnName] = function(...args) {
// ...
}
But what if I cannot access the window object? Like inside of Worker, for example.
Is it possible to do it without window and without eval()?
You can use self. It is property in both window and Worker scope that references back to their global scope. MDN/Window/self

object name is same as variable [duplicate]

This question already has answers here:
"Variable" variables in JavaScript
(9 answers)
Closed 3 years ago.
Let's say I have an object
const myArray = {
a : "hello"
}
and I have a string with the same name of that object
like
var type ="myArray";
when I do console.log(type);
output: myArray
but I want to out that object to the console which has the same name as the value of variable type.
How should I do that?
Thanks in advance
If it's a global variable, it will be stored in window.
So, you can do something like console.log(window[type]) to access to value.

Why {} !== {} in Javascript [duplicate]

This question already has answers here:
Why are two identical objects not equal to each other?
(9 answers)
Closed 6 years ago.
I was going through Map Documentation on MDN. In Examples, under Using Map Object, Object Literal - {} is used as key to store value. But, the value in Map can't be retrieved using Object Literal.
I verified this in Browser Console and found that Object Literal is not equal to itself. Also, the Function Expression - function() {} is not equal to itself.
I couldn't find the reason behind this. If required, I can ask a different question for Function Expression.
Each time you do {}, it creates a new empty object, so when you do {} == {}, you're comparing two different objects. This comparison is done by reference, so it returns false.

Creaating a javascript object with computed name [duplicate]

This question already has answers here:
"Variable" variables in JavaScript
(9 answers)
Closed 6 years ago.
how would I make an object in javascript with a computed name.
Context:
I am making an add-on that will sit in the browser, log each hostname visited, create an object named after each one. I'm replacing "." with "_"
So for example on this site it would create a stackoverflow_com object.
is this possible?
another example would be
var 1+1 (with the variable actually being 2)
I know brackets make this possible with properies but I don't know how to do it with the name itself.
You need to store them either in a global object (like window) or preferably an object of your choosing
var mySites = {};
mySites["stackoverflow_com"] = "foo"; // access as mySites.stackoverflow_com or mySites["stackoverflow_com"]
mySites[1+1] = "bar"; // access as mySites[2];
More info: JavaScript property access: dot notation vs. brackets?

Categories