I am working on some code, and I have stumbled across something I am unfamiliar with.
export const doSomething = () => [ someFunction(), bind(stuff, stuff, stuff) ];
I have never seen an arrow function with a square bracket like that, has anyone else? If so what is the meaning of how it works?
There are two ways to declare an arrow function: Without curly braces {} . With this syntax, the arrow function has an implicit return. For example, the below arrow function returns 42, even though there's no return .
It's a new feature that introduced in ES6 and is called arrow function. The left part denotes the input of a function and the right part the output of that function.
What It Is. This is an arrow function. Arrow functions are a short syntax, introduced by ECMAscript 6, that can be used similarly to the way you would use function expressions. In other words, you can often use them in place of expressions like function (foo) {...} .
A postfix expression (operand followed by the operator) followed by an expression in square brackets [ ] is a subscripted designation of the array element . The definition of the array subscript operator [ ] is that if a is an array and i is an integer then a[i] =*(a+i) .
An arrow function expression has a shorter syntax than a function expression and does not have its own this, arguments, super, or new.target. These function expressions are best suited for non-method functions, and they cannot be used as constructors.
Square brackets, as in [ π] = 3, are sometimes used to denote the floor function, which rounds a real number down to the next integer. Respectively, some authors use outwards pointing square brackets to denote the ceiling function, as in ]π [ = 4.
Arrow functions do not have their own arguments object. Thus, in this example, arguments is simply a reference to the arguments of the enclosing scope: In most cases, using rest parameters is a good alternative to using an arguments object.
An arrow function cannot contain a line break between its parameters and its arrow. However, this can be amended by putting the line break after the arrow or using parentheses/braces as seen below to ensure that the code stays pretty and fluffy. You can also put line breaks between arguments.
This code means that your function doSomething
returns an array when
[0]
element - the result of execution of function someFunction()
and
[1]
element - the result of execution of function bind(stuff, stuff, stuff)
.
This is a shortcut for:
export const doSomething = () => {
return [ someFunction(), bind(stuff, stuff, stuff) ]
};
But be careful if you want to make a shortcut for returning objects. You have to wrap objects in parentheses ()
, like this:
export const doSomething = () => ({ name: 'John' })
.
It's just returning an array.
You might use it with a destructuring assign e.g.
const [someResult, boundStuff] = doSomething()
Or just like any old function e.g.
const something = doSomething()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With