Javascript use variable name to thread a function? [duplicate] - javascript

This question already has answers here:
Use JavaScript variable as function name?
(5 answers)
Closed 7 years ago.
In GSC, you are able to make a variable become the name of a function that you thread. It looks like this:
variable = "pizza";
[[variable]]();
the engine then reads it like:
pizza();
my question is, is it possible to do that in javascript as easily or do I have to make if/else/switch statements for it?

my question is, is it possible to do that in javascript as easily or
do I have to make if/else/switch statements for it?
If you want to use the safe, fail-proof way, then you can access such variables only in two contexts.
If the variable is in global context, in the case of which, you can do window[variable]();
Else if the variable is a property of an object, in the case of which, you can do obj_name[variable](), basically anything that can be accessed via bracket notation. window is an object too.
Then there's always the dirty way:
You can use highly evil eval like eval(variable + "()") or you can use the Function constructor in the same way. Note however that both the methods can be misused and are highly advised against.

Related

Which has better performance?Accesing global variable directly from a function or assigning it to another variable? [duplicate]

This question already has answers here:
How do you performance test JavaScript code?
(24 answers)
Closed 2 years ago.
Here I have two examples of accessing global variables;
basic example of my global variable
globalVar:any={
a:5,
b:3,
c:4
}
Ex.1
function accesVar(){
return this.globalVar.a*=2
}
Ex.2
function accesVar(){
let _var=this.globalVar.a;
return _var*=2
}
In this example global variable doesnt seem a good practise and I dont use them when I dont need to share variables with more than one function.Considering in general and real world(much bigger data),which would be best approach and has better performance?
Both have different importance.
Example one is direct use of globle variable. Saves you extra declaration.
Example two is useful when we need to use the global value many times inside the function.

Seeking an alternative to arguments.callee.name for use in console.log [duplicate]

This question already has answers here:
How to get function name in strict mode [proper way]
(2 answers)
argument.callee.name alternative in the new ECMA5 Javascript Standard [duplicate]
(4 answers)
Closed 5 years ago.
'use strict';
function foobar(){
console.log(arguments.callee.name); // foobar
}
I hear this once worked, but now it's deprecated. There seem to be other alternative to it for other situations, and I've seen lots of references to using function.name, but having to enter foobar.name defeats the purpose. Within a class I can use this.constructor.name and get the name of the class. I'm looking for something like that for the name of a function.
Why this is not a duplicate to any existing question since ES6: one answer to one similar question talks about using named functions, but this is a named function. To another question the answer is to use function.name - but this question is basically asking if there is a way to .log the name without knowing the name. To another question the answer was to use something like ()=>Function.caller.name but that is non-standard.
If you're using ES6, the function now has a property 'name'
Using your example:
function foobar(){
console.log(foobar.name); // foobar
}

Define & assign variable with function Node [duplicate]

This question already has answers here:
Does node.js have equivalent to window object in browser
(2 answers)
Closed 6 years ago.
I know with browser-run JavaScript, I could use window[varName]=value; to set global variables. I seem to remember there being a function to accomplish this in Node JS, but I'm not sure what it is.
If it helps, I'm aiming to set all the properties of an object as separate own variables.
In Node.js the global variables are stored in the global object, I think...
Then it'd be so:
global[varName] = value;
// or...
global.varName = value;
I think you're confusing something. Maybe you want to initialize a object constructor expression.
{ property: "string, or anything else" }
// this expression returns a object that can be assigned
// everywhere, but when assigned, turns a reference
If you want to get/set properties in this object you must do the same thing you were doing before, index the object with the keys [...] or ., then you can optionally assign (set) the object's property with =, else it'll be returned.

JavaScript have a variable in a variable name [duplicate]

This question already has answers here:
Use dynamic variable names in JavaScript
(19 answers)
Closed 9 years ago.
I have a variable varName, which has the contents of "myvar". I want to create a variable that has the name of variable and the contents of varName, so the name would be variablemyVar.
How do I do this?
You can do
window["variable" + varName] = 42;
console.log(variablemyvar); // 42
This will make the variable global. Instead of window, you can use any scope (object), i.e.
var myObject = {
someOtherVariable: 1337
};
myObject["variable" + varName] = 42;
console.log(myObject.variablemyvar); // 42
However, you should avoid doing this altogether whenever possible. The reason for this is that code like this breaks easy – say you want to rename the variable later on. Most likely you will overlook this, even the IDEs will. Another reason is that code minifiers will fail to minify this, if you set them to be "aggressive", it will even break the code during minification.
Lastly, especially using the first example with window, global variables are considered bad practice and should always be avoided, no matter how you declare them.

Why can't one set an alias to document.getElementById()? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
JavaScript function aliasing doesn't seem to work
Set document.getElementById to variable
Code would be more efficient if this was possible:
var min = document.getElementById;
and then call document.getElementById() using min().
Not trying to write minified code but in this particular case one can reduce scope lookup and shorten some lines.
Is this a syntax issue or a limiation on the language?
When you call foo.bar() then this is set to foo inside bar
When you copy foo.bar to window.bar then call window.bar(), this is set to window.
getElementById has to operate on a DOM document object.

Categories

Resources