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

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.

Related

Javascript 'not defined' error when declaring default function parameter matching to a constant? [duplicate]

This question already has an answer here:
Scope of Default function parameters in javascript
(1 answer)
Closed 4 years ago.
Just discovered some weird error that reads:
Uncaught ReferenceError: symbol is not defined
Code in question:
const symbol='tNEOUSD';
function get_position(symbol=symbol){
console.log(symbol);
}
get_position();
How come it's not defined? That's a really weird!
On the other hand if I use a different parameter name it works just fine:
const symbol='tNEOUSD';
function get_position(sym=symbol){
console.log(sym);
}
get_position();
Does anyone know why this happens?
You cant use a variable which is already declared outside as a parameter.

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

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.

JavaScript property access that throws an error [duplicate]

This question already has answers here:
Why does reading a property sometimes throw an error in javascript?
(5 answers)
Closed 1 year ago.
In which specific cases does property access throw an error in JavaScript?
In Node.js, this prints undefined:
x = 3
console.log( x.thing );
This throws an error:
x = null;
console.log( x.thing );
What exactly is the semantics here? Property access is normal behavior for almost all values—even functions—but on undefined and null it throws an error.
I can't for the life of me find confirmation that those are the only cases. Can anybody confirm that?
undefined and null are not references to objects, nor are they primitive values that can be implicitly boxed in object wrappers. Thus, any attempt to reference a property is going to fail.
When you use a number (3), the runtime boxes that as a Number instance, but of course there's no "thing" property so the value is undefined.
Also, functions are first-class objects, so references to properties on functions are not really "weird" in any sense.

Why does setTimeout(location.reload) throw a TypeError? [duplicate]

This question already has answers here:
Why can't I pass "window.location.reload" as an argument to setTimeout?
(4 answers)
Closed 6 years ago.
I'm trying to understand the strange behavior of this code:
window.setTimeout(window.location.reload, 200);
In Firefox this throws a TypeError:
TypeError: 'reload' called on an object that does not implement interface Location.
In Chromium this throws another TypeError:
Uncaught TypeError: Illegal invocation
These two alternatives work fine:
window.setTimeout('window.location.reload()', 200);
window.setTimeout(function(){window.location.reload()}, 200)
Why?
This is due to the fact that window.location.reload will be called out of context, so the actual function reload() does not have any reference to the location object.
In JavaScript, if you call a function foo.bar(), the context is foo, so this refers to foo within the function bar.
However, if you assign var a = foo.bar and then call a() (which is still the same function), it will have no context, so this will be undefined. This is what happens when you pass the function as a method parameter.
The usual way to work around this issue is to bind the context:
window.setTimeout(window.location.reload.bind(window.location), 200);
This ensures that the function will always be called in the context of window.location, no matter how it is called.
There is a nice example in the Mozilla docs.

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.

Categories

Resources