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

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.

Related

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
}

Mixing javascript and jquery [duplicate]

This question already has answers here:
What's the difference between '$(this)' and 'this'?
(7 answers)
Closed 6 years ago.
$(this).append('hello');
The above could be a line nested in some function of jquery. I know that this is a correct line, but I was wondering whether you could mix javascript with jquery and write it down like this.
this.append('hello');
The only difference is that in the second line of code the this keyword is not written inside the jquery method, but this should still work because the this keyword itself automatically stores the location of the node at which the function is aimed. Right? Well apparentely it does not work, so when and why do can't you use the this keyword alone?
The only difference as far as I understand is the wrapper, which is the jQuery object: https://learn.jquery.com/using-jquery-core/jquery-object/. This wrapper exposes jQuery methods on the element, whereas just using this returns the element, but does not expose those same methods. As a simple example, this will return the textContent of the div,
$('div').click(function() {
console.log($(this).text());
})
Where as this will not
$('div').click(function() {
console.log(this.text());
})

Dynamically call function in javascript [duplicate]

This question already has answers here:
Calling function inside object using bracket notation
(2 answers)
Closed 6 years ago.
I rather have a seemingly trivial issue, but am not able to figure out an efficient approach.
I have a list of about 50 functions to be called such as :
globalClient.funcA(...)
globalClient.funcB(...)
globalClient.funcC(...)
My code should ideally dynamically create the name of the function (funcA / funcB/ funcC and then proceed to actually call that function. My approach below does not work (please note that these aren't exactly the actual names of the functions. I'm only giving these arbitrary names for simplicity of understanding):
var functionName = 'func'.concat('A');
globalClient.functionName
The second line is where it errors out. Now JS thinks that functionName itself is the name of the function. What I want it to do is resolve functionName to funcA and then call globalClient.funcA(...) instead.
I've thought about implementing a switch / case for this but I'm sure there is a far simpler appraoch. Any ideas?
You could use the bracket notation as property accessor.
globalClient[functionName]()
You can use the [ ] operator for accessing the properties.
var globalClient = {
funcA: function(){
console.log('funcA is called');
}
}
var functionName = 'func'.concat('A');
globalClient[functionName]();

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

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.

What is meaning of "(function(window, undefined){})(window)" in JavaScript? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How does this JavaScript/JQuery Syntax work: (function( window, undefined ) { })(window)?
(function(window, undefined){})(window);
What is the meaning of this code? I have seen it in many documents, especially in jQuery documents.
How does this work, and why is it defined thus?
You are scoping a piece of code..
By.
Defining it within an anonymous function //function(){...}
Executing it. //(function{})(args)
Also, passing the window parameter allows for faster resolution of the meaning of that variable within your block of code.

Categories

Resources