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

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

Related

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

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

I can't read a member variable from within another member function [duplicate]

This question already has answers here:
How to access the correct `this` inside a callback
(13 answers)
Closed 6 years ago.
I have a trouble about scope, I knew the solution but this isn't the problem.
I want to know why the member variable xhrObj unreadable from within an another member function, although that variable is global variable for that member function ?
It is happening because xhrObj function onreadystatechage is asynchrous in nature and when it return after complete call this context is different inside the onreadystatechage() and hence this.xhrObj is not different.

How do I make a global variable using local variables? [duplicate]

This question already has answers here:
Accessing an object property with a dynamically-computed name
(19 answers)
Closed 6 years ago.
I am making a new video game and I need help with a purchase feature.
I want to use local variables to "create" a global variable. For example:
function example(apple,banana) {
return unicorn.apple.banana.text
}
example(taco,burrito);
should return whatever unicorn.taco.burrito.text equals.
For this you need to use the Array Notation.
function example(apple, banana) {
return unicorn[apple][banana].text
}
example(taco, burrito);
Because you need to replace apple and banana, with their values.

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?

Equivalent of window["myString"] for Local Variable [duplicate]

This question already has answers here:
How can I access local scope dynamically in javascript?
(4 answers)
Closed 8 years ago.
Let say I got a string with the name of the variable I want to create.
If I want to create it with a global scope, I'd use window["myString"] = 100; or global["myString"] = 100; on Nodejs.
Is there a way to do the same but create the variable with local scope? (Ex: Inside a function)
EDIT: Note: The goal is to access the variable with its name directly. Ex: myString
I already know that I could easily create a object that would have the value as an attribute. Ex: obj.myString. But this is not what I'm looking for.
The local scope is not exposed as the global object is.
However...
function someFunction() {
var locals = {};
locals['someLocalVar'] = 'local var';
}

Categories