This question already has answers here:
How to interpret function parameters in software and language documentation?
(4 answers)
Closed 2 years ago.
I'm trying to get a good sense of reading documentation instead of asking so many questions here and there.
The first thing i'm confused of is the symbols.What do the symbols below mean?
example) app.use([path,] callback [, callback...])
reference source:https://expressjs.com/en/5x/api.html#app.use
Question1&2 can be solve by the similar posting.
Another example is like
bisector.left(array, x[, lo[, hi]])
what does x[, mean?
what does **lo[,**mean?
what does ,hi] mean?
reference source: https://devdocs.io/d3~5/d3-array#bisect
It would be very grateful if anyone could help me to under stand these cryptic symbols.
[xxx] means that this parameter is optional. You can use it or omit it.
[callback] is also an optional parameter. In this case it's a function that will be called under some circumstances.
Nested bisector.left(array, x[, lo[, hi]]) means that you need to have some options before you can specify others. For example, you cannot specify lo in this case without specifying hi first.
Related
This question already has answers here:
In Typescript, what is the ! (exclamation mark / bang) operator when dereferencing a member?
(5 answers)
Closed 7 months ago.
I've seen somewhere written in the code fixture.componentInstance.dataSource!.data = [];
I want to know the meaning of dataSource!.data
I know about the meaning of question mark(?) before dot(.) like key1?.key2 but an Exclamation(!) before dot(.) !. expression makes me curious.
Many thanks in advance!!
It's called the Non-null Assertion Operator
https://www.typescriptlang.org/docs/handbook/2/everyday-types.html#non-null-assertion-operator-postfix-
It removes null and undefined, allowing you to assert that you know there will be a value. Usually this is a code smell, something that should give you pause to consider other methods so that you can avoid using it. Type narrowing would be preferred. But in some specific instances, this operator can be a useful tool.
This question already has answers here:
Rules for unquoted JavaScript Object Literal Keys?
(6 answers)
What characters are valid for JavaScript variable names?
(12 answers)
Closed 1 year ago.
I am seeing it crop up more and more in code I am going through on a new project (can't share due to contractual reasons) where Ill see something like:
{
prop1: value$ref,
$prop2: null
}
I have see ${prop3} before, but never an example without the brackets. Can anyone provide direction as to what the method is, or the operator is or whatever the case?
This question already has answers here:
What do curly braces inside of function parameter lists do in es6?
(3 answers)
Closed 2 years ago.
I came accross this line of javascript code
let {max}=Math;
that allows you to do this:
let a=max(1,2) //2
Without using the Math object like this:
Math.max(1,2)
I don't know where to find the documentation of this syntax because all I have is that line of code and not a single clue to help me search on google so anyone knows about this practice ?
This is called Destructuring Assignment and you can find documentation here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment
What you see there is called Destructuring assignment.
You'll find plety of examples when you Google for Destructuring assignment JS ;)
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment
Anyway, in this particular case you assign the property max (a function in this case) from Class Math.
This allows you to just call max directly.
Imagine having an object DestructTest with a property x
Then you can get the property x by just typing const {x} = DestructTest
Instead of const propX = DestructTest.x
JavaScript takes care for you to get the wanted property for you if you just ask for the right name ;)
Hope this makes things a bit more clear.
Cheers
This question already has answers here:
What do the brackets around the arguments mean when reading documentation for a method? [duplicate]
(3 answers)
Closed 6 years ago.
I've seen this a lot and most of the time I can figure out what it means, but there have been times when I was a bit confused. Looking at documentation mostly on jquery i see something like this:
.toggleClass( function [, state ] )
what does the bracket notation with the comma and state mean exactly?
Any info would be awesome.
Thanks!
Everything between the brackets [] is optional.
In your case it means that state can be omitted, and the function will still work.
Optional parameters.
Those marked inside such as state in your case can be omitted and the function will not throw an error.
For another example:
jQuery.ajax( url [, settings ] )
Means both
jQuery.ajax( "www.google.com" )
and
jQuery.ajax( "www.google.com" , {dataType: 'mycustomtype'} )
are valid.
This question already has answers here:
Is there a way to provide named parameters in a function call in JavaScript?
(12 answers)
Closed 7 years ago.
Calling a Javascript function with something like
someFunction(1, true, 'foo');
is not very clear without familiarity with the function.
I have seen and used the style where comments are inserted to name the arguments:
someFunction(/*itemsToAdd*/1, /*displayLabel*/ true, /*labelText*/ 'foo');
But when it gets beyond 3 or more parameters, it seems better to pass arguments in a JSON object which makes it order-independent, and allows default values to be provided in the called function
someFunction({'itemsToAdd':1, 'labelText':'foo', 'displayLabel':true});
My question is; what is the general practice in the industry, and are there overriding reasons for not using any of these methods. Lint for example does not like the second method.
Personally I'd just grep the function name and take a look at the comment associated with it. Well maintained code will have a comment above the function explaining what the arguments are and what it does with them, and you can just paste that above the function call if you need to explain why your arguments are the way they are.
Using a JSON to pass arguments seems like a way to add unnecessary parsing overhead and to possibly confuse the maintainer - just add more fields and pass in NULLs to the fields where you want default values, and you can explain why you're passing in NULLs in the call comment instead of just having them not appear in the JSON.