Can you set an undefined variable as a function parameter? [duplicate] - javascript

This question already has answers here:
Undefined variable as function argument javascript
(2 answers)
Closed 8 years ago.
I am building a function to match types of variables. It will be an alternative to typeof <var> === "something".
My function call looks like this : is("some text")["string"]. It returns true, or is([])["element"]. Now it returns false, But I have an issue with it.
For example, if I try to send an undefined variable like "undefVar" to a function I am expecting something like this: is(undefVar)["undefined"], but I get an error which says that "undefVar" is not defined.
Can I somehow make my function work using undefined variables? Is this possible at all?
p.s: I have to use this function a lot so it seems (for me) that it would be better to use something like this : is(var)[type] as opposed to typeof var === type.

typeof is special on nonexistent variables. You cannot mimic it with a function.

No, you cannot blindly pass undefined variables to your function from the call site and globally change the behavior of the JS engine to accomodate this.
typeof is a built-in operator and is not bound by the rules of "common" functions, which is why it can do things that custom functions cannot.
Finally, I strongly disagree that it would be practically preferable to use an alternative syntax even if that were possible.

Related

What is the best way to write functions that does not use its parameter in JS? [duplicate]

This question already has answers here:
Standard conventions for indicating a function argument is unused in JavaScript
(6 answers)
Closed 9 months ago.
Specifically, I am using react-semantic-ui's Select Form, which takes an onChange function. So I have defined my function as such:
const handleInputChange = function handleInputChange(event, data){// does something with data}
(don't mind the ugly syntax, that's airbnb style guide)
I am doing something with data, but I am not using the event parameter at all. What would be the best practice in this case, since it is generally a bad practice to declare a variable without using it?
You must declare a parameter for every argument before the one you want to use.
The idiomatic approach to marking a parameter as unused is to prefix its name with an underscore.
function handleInputChange(_event, data){
Some linters will recognise this and not emit a "unused variable" warning.
For example ESLint has a argsIgnorePattern option (but it also has a args option that lets you specify after-used).
Actually by using Js es6 you could code without unused argument:)
function handleInputChange(...[, data]) {
console.log(data);
}
handleInputChange('event', 123);
And bellow is a similar question you could check out:)
Standard conventions for indicating a function argument is unused in JavaScript

Best approach to avoid naming collisions in object with javascript [duplicate]

This question already has answers here:
Is the underscore prefix for property and method names merely a convention?
(6 answers)
Closed 2 years ago.
Lets say I have an object with a getter and a setter.
const obj = {
a: 0,
get a(){
return this.a;
}
}
I would get an error Maximum call stack size exceeded at Object.get a [as a]. I could name the a variable something different but then it gets confusing after a while.
I could prefix it with an underscore or something like this:
const obj = {
_a: 0,
get a(){
return this._a;
}
}
I was wondering if there is an industry standard for handling this? Is using underscores not the right approach because they are symbolic of other things? What is the recommended approach for handling this or is it one of those things that is up to the developer?
I just don't want to go down the path of doing this the wrong way and can't really find a definitive answer on the matter.
Thanks.
Your first example doesn't work, because the getter named a will end up being the only property on your object. When you call this.a you're actually calling the getter again and so you end up with a stackoverflow.
Using _ as prefix for underlaying variables is perfectly fine. This actually lines up with other languages.

Angular expression is unable to tell variable type using constructor property [duplicate]

This question already has answers here:
AngularJs Inline Check if an array check
(3 answers)
Closed 6 years ago.
Using an angular expression, I tried to show if a scope variable is an array.
I tried to use variable.constructor === Array for checking the same, but in expression always showing false as result.
When I used a scope function to return variable.constructor === Array, I got the correct result.
Can someone tell, why expression behaves in this manner.
Also, how same check can be done as an inline expression statement.
Checkout the Plunker demonstrating same problem, here
Thanks.
There is no variable named Array in your scope, so the expression variable.constructor === Array is basically returning the result of variable.constructor === undefined.
That's why it's always false.
There is nothing known as Array, so you are essentially comparing a value to undefined.
To test that a value is an array, you can use the in-built Angular function called isArray().
See the answer here for more information: Angular expression to check if model is array or object
You'll have to use Array.isArray(variable) in JS.
Further info on MDN

Arguments of self executing function [duplicate]

This question already has answers here:
How does this JavaScript/jQuery syntax work: (function( window, undefined ) { })(window)?
(5 answers)
Closed 9 years ago.
(function (global, undefined) {
... some code which doesnt use arguments array
} (this));
I often see module pattern done in this way.
I really question why there's a second argument undefined?
Are these examples buggy or is there a special meaning of undefined here?
undefined is a global property that is widely used. In older versions of JavaScript it is possible to change the value of it (for example, to true). This generally breaks everything.
By changing its scope to be local to the "module" (i.e. the function), other modules are prevented from interfering with it.
This allows code to safely use undefined instead of having to use global.undefined.
MDN Reference

Why does jQuery file have two parameters in its function but receives only one parameter [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How does this JavaScript/JQuery Syntax work: (function( window, undefined ) { })(window)?
I am trying to learn about js scopes and anonymous functions. I have tried to read the jQuery.js file and it looks like this:
(function( window, undefined ) {
...
}(window));
Why does it have in the function params undefined when no parameter is being passed to it when it is executed?
This method is used so you can be sure that no one has previously redefined undefined value with something like
var undefined = true;
or with other tricky/evil assignments outside the jQuery scoped function. So, inside that function every comparison done against undefined is safe.

Categories