JavaScript - undefined passed in as a parameter *name*? [duplicate] - javascript

This question already has answers here:
What is the purpose of passing-in undefined?
(3 answers)
Closed 5 years ago.
I have just come across a function which accepts a parameter actually called 'undefined'. It is something like this:
(function($,undefined) {
// Do something
})(jQuery);
Either I'm going crazy, or there is no logical reason for this to be here as, well, undefined is undefined is undefined. Please can someone confirm either way? Thanks.

That is a classic trick to have an undefined variable to check against, typically:
if (someVar === undefined) {}
// instead of:
if (typeof someVar === 'undefined') {}
Notice that the wrapper IIFE does not pass any second argument, making the undefined parameter effectively undefined.

Related

Javascript Syntax Colon - Need Help (Not Json object or ternary) [duplicate]

This question already has answers here:
What does the colon mean in Javascript after function?
(2 answers)
Closed 1 year ago.
I am going through source code of Vue.js. In almost all the function declarations I find a new way of defining functions
function isStringStart (chr: number): boolean {
return chr === 0x22 || chr === 0x27
}
Can somebody explain me what is this kind of function declaration called?
That's a type declaration. :boolean means basically, that the isStringStart function must return a boolean value. The same with the argument's type declaration. chr: number means, that the function accepts one argument, which has to be typeof number.
If the requirements are not fulfilled (not proper arguments are passed or wrong value is being returned), the typechecking library you are using will throw an error.

Why does calling a method with parenthesis, eg. (obj.func)(), still set `this`? [duplicate]

This question already has answers here:
How does JavaScript determine when to give a function call a "this" context? [duplicate]
(2 answers)
Closed 8 years ago.
What exactly is the parsing rule in JS that results in the following:
Let's say we have this function
getThis = function(){
return this;
}
These all work as expected using the "previous dot" rule:
getThis(); //=> Window
obj = {getThis: getThis};
obj.getThis(); //=> obj
getThisTwo = obj.getThis;
getThisTwo(); //=> Window
However, this surprises me:
(obj.getThis)() //=> obj ...WAT
My intuition would be that it would behave exactly like the 3rd example (getThisTwo). Ie, the part in parentheses is evaluated, which returns an anonymous function, which is then invoked. My expectation then is that this would be Window, not obj.
Is this a special case, or is my understanding of how this is resolved faulty?
(Edited to make the reason for my confusion clearer)
Yes. The value of the this context of a call depends on the type of the function invocation.
In your case, it's a method invocation - a function that is called by a property reference. And yes, parentheses do not evaluate a property reference.
See also Nature of JS bound functions and function invocation operator and this very good answer for details.

What is typeof(NaN) in Javascript? [duplicate]

This question already has answers here:
Why does typeof NaN return 'number'?
(21 answers)
Closed 9 years ago.
I just happen to stumble upon a code which checks the typeof of a varable passed to it just like this.
function myNaN(b){
if(typeof(b) == 'number'){
// execute some code
}
}
Whenever I call this function it works fine and passes the if condition if number is being passed.
However when I pass a NaN (which is the output of some other function) to this function the if condition returns true.
My question is it correct that typeof(NaN) == 'number' ? If so, why? Isn't it confusing?
Just try running console.log(typeof(NaN)); in browser console to see what I mean.
Yes, typeof(NaN) is number. You can check if the value is NaN specifically using the function isNaN.
Why don't you use the: "isNan("1234")" function ?
Here is some link if it helps: http://www.w3schools.com/jsref/jsref_isnan.asp

Why does one reuse `undefined`? [duplicate]

This question already has answers here:
How does this JavaScript/jQuery syntax work: (function( window, undefined ) { })(window)?
(5 answers)
Next parameter is 'undefined' in jQuery, why? [duplicate]
(3 answers)
Closed 9 years ago.
In John Resig's slideshow on how he was building jQuery 1.4, he mentioned a point where he added an undefined variable to the jQuery closure because "we can re-use (the variable)".
undefined is not an ordinary variable:
> var undefined = 4
undefined
> undefined
undefined
Therefore, we know that undefined is not a variable. So why would an undefined be re-undefined in the jQuery source?
Because in some JavaScript engines it's possible to set undefined to a value. This is to make sure undefined is really undefined.
Additionally to +Rocket Hazmat's answer, you can reduce the file size after compression a bit, when your code uses undefined frequently. That's because a local variable undefined may have its name mangled by the compressor, while the global undefined may not:
foo === undefined;
// ^----- don't touch this, put "undefined" in the compressed result
(function (undefined) {
foo === undefined;
})();
// may however be mangled to
(function(u){foo===u})();

Passing undefined parameter to function - check if variable exists [duplicate]

This question already has an answer here:
Error when passing undefined variable to function?
(1 answer)
Closed 9 years ago.
Consider the following Javascript:
function getType(obj){
return(typeof(obj))
}
alert(typeof(obj)) //alerts "undefined" correctly
alert(getType(obj)) //throws an error: ReferenceError: obj is not defined
Why might this be happening? Is there any workaround? I am trying to write a function which checks if a variable exists.
The problem is nothing to do with typeof. The problem is that you cant pass undefined variables to functions.
function doNothing(obj){
}
doNothing(obj);
This code too results in the error: Uncaught ReferenceError: obj is not defined
So it doesn't matter what code you write inside your function, as it won't be called. The error happens before the function call.
typeof is an operator, not a function.
This is why it does not behave in the same way as functions.
typeof is an operator, not a function, and therefore has powers that a function can't have. There's no way to do what you're trying to do.
Your function fails as soon as you try to pass an undefined object. You can't encapsulate the typeof() function. Well, you can, but it will always throw errors when passed undefined objects.

Categories

Resources