Javascript documentation notation [duplicate] - javascript

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.

Related

JavaScript (function(){...}( )) and (function(){...} ) ( ) [duplicate]

This question already has answers here:
Location of parenthesis for auto-executing anonymous JavaScript functions?
(4 answers)
Closed 7 months ago.
I've inherited a project with some Java Script. I know nothing about js. There are some auto-build steps involved, the result of which is an anonymous function. The version is version control is different from the version I build, even though the environments were supposed to be the same.
One version is, void parameter list inside the evaluation brackets:
(function(){...}( ))
The other version is, void parameter list outside the evaluation brackets:
(function(){...} ) ( )
Are these two forms technically the same? Is one form technically an error? Is either form actually an error? Or what?
In this case, both are equivalent and valid.
Note that for the first option, when the outer parentheses are excluded, that will result in a SyntaxError
function(){...}() // error
(function(){...}()) // no error
(function(){...})() // no error

How to understand symbols in documentations [duplicate]

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.

Javascript syntax []() and ()() [duplicate]

This question already has answers here:
How does this object method definition work without the "function" keyword?
(2 answers)
Closed 6 years ago.
I'm having trouble understanding these javascript syntaxes. In the block of code below, on the second line. The square bracket is quickly followed by a round bracket or parentheses which I suspected is used to get arguments. I do not understand how this two is being chained to form an expression and what it means.
export const recipeCount = createReducer(0, {
[types.ADD_RECIPE](state, action){
return state + 1;
}
});
Also on this line, the connect method takes in two arguments, (state) => {return {}} and mapDispatchToProps . Then it is quickly follwed by () with an argument. At first, i though it was some of object casting in java but that doesn't make sense.
export default connect((state) => {return {}}, mapDispatchToProps)(AppContainer);
The code executes fine and produces expected result. I just don't understand what is going on. Pls Help, would be glad to get answersre accompanied with links to pages i can read for better understanding. Thanks.
Answers are in the comment to the question. Had to copy them out again, so i can mark the question as answered and close it.
"Not sure what's going on with the first one. For the second one, connect() is a function that returns a function so the second () is to immediately call that returned function." – Ouroborus
"The first one is a dynamic object literal property that is also an object method. I find this not readable at all. I would re-write that one. – Davin Tryon"
and also a link to Computed property names to make it clearer from – Denys Séguret
Thanks Guys.

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]();

What is [, ... ] in function/method documentation? [duplicate]

This question already has answers here:
How to interpret function parameters in software and language documentation?
(4 answers)
Closed 6 years ago.
I have come across this type of syntax in Node.js. Can anyone please explain what [, is in Node.js/JavaScript?
Examples of syntax I have seen.
assert(value[, message])
const name1 = value1 [, name2 = value2 [, ... [, nameN = valueN]]];
You're looking at function definitions in documentation, and what you're seeing is a conventional way to show that one or more parameters are optional. It is not actually valid JavaScript syntax.
For example, assert(value[, message]) means that the value parameter is required, but message is optional. You could not actually put assert(value[, message]) in your code; it would trigger a syntax error.

Categories

Resources