Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a name for this syntax getSomeValues()[0]?

In some languages, if a function returns an array, then as opposed to storing the array in a variable then retrieving a single element, like this:

var someValues = getSomeValues();
var firstElement = someValues[0];

You can use the array index notation directly after the function call to retrieve the element of the returned array instead, like this:

var firstElement = getSomeValues()[0];

What is this construct or syntax known as? Does it have a special name at all?

like image 237
BoltClock Avatar asked Jul 29 '10 11:07

BoltClock


2 Answers

You can use the array index notation directly on the function call to retrieve the element

var firstElement = getSomeValues()[0];

What you are doing is accessing the indexed element of the array/collection returned by the function. You are not directly accessing an index in a function.

I would call this "chaining" an indexer to the return value.

like image 85
Oded Avatar answered Nov 20 '22 16:11

Oded


Array dereferencing

like image 2
Eton B. Avatar answered Nov 20 '22 14:11

Eton B.