Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What do square brackets around a property name in an object literal mean?

I've been writing in JS for a while and have not used this form:

  dist: {     files: {       [bpr + 'lib/Monster.min.js']: ['<%= concat.dist.dest %>']     }   } } 

the

[]:[] 

it works, I just have not used it or seen it before.

like image 544
cade galt Avatar asked Jan 16 '16 19:01

cade galt


People also ask

What do square brackets mean in syntax?

The square brackets themselves are not typed unless they are shown in bold. | A vertical bar that separates two or more elements indicates that any one of the elements can be typed. < > Bold angle brackets are part of the syntax, and must be typed unless indicated otherwise.

What do square brackets mean in JavaScript?

The [] operator converts the expression inside the square brackets to a string. For instance, if it is a numeric value, JavaScript converts it to a string and then uses that string as the property name, similar to the square bracket notation of objects to access their properties.

Why are words sometimes in square brackets?

Use square brackets to include words within a quote that are not part of the original quote. For example, if a quoted passage is not entirely clear, words enclosed in square brackets can be added to clarify the meaning.

What are the two types of notations does object property have?

A method is a property that can be called (for example, if it has a reference to a Function instance as its value). There are two ways to access properties: dot notation and bracket notation.


1 Answers

Only recently with ES6. They are called "computed property names"

From MDN:

Starting with ECMAScript 2015, the object initializer syntax also supports computed property names. That allows you to put an expression in brackets [], that will be computed as the property name.

like image 109
Matt Avatar answered Oct 15 '22 19:10

Matt