Ishmael7
Ishmael7

Reputation: 173

Confusion about const declaration inside object

I came across this piece of code here, and I noticed that const data is declared inside the brackets. I thought you had to use the key: value format inside the brackets when creating an object. How does this work?

data = {
  const data = await d3.tsv("https://gist.githubusercontent.com/mbostock/8033015/raw/01e8225d4a65aca6c759fe4b8c77179f446c5815/unemployment.tsv", (d, i, columns) => {
    return {
      name: d.name.replace(/, ([\w-]+).*/, " $1"),
      values: columns.slice(1).map(k => +d[k])
    };
  });
  return {
    y: "% Unemployment",
    series: data,
    dates: data.columns.slice(1).map(d3.utcParse("%Y-%m"))
  };
}

Upvotes: 3

Views: 74

Answers (2)

oktapodia
oktapodia

Reputation: 1758

The code above is not a valid javascript code.

observableHQ is using its own parser to achieve that https://github.com/observablehq/parser the code is translated to the following:

const chart = (arguments) => {
  // code...
}

Upvotes: 2

Nicholas Tower
Nicholas Tower

Reputation: 85271

That is not valid javascript. The reason it works is that Observable has its own syntax. It's deliberately designed to be similar to javascript, but it isn't actually javascript. You can read more about this here:

https://observablehq.com/@observablehq/observables-not-javascript

Upvotes: 3

Related Questions