Button and dot notation in compiled CoffeeScript

I have this piece of code in a Node / Express application:

app.use "/static", express.directory("#{__dirname}/public") app.use "/static", express.static("#{__dirname}/public") 

It compiles as follows:

 app.use("/static", express.directory("" + __dirname + "/public")); app.use("/static", express["static"]("" + __dirname + "/public")); 

As a curiosity, I wonder: why is the dot-note used for the first call and the brackets for the second call?

+4
source share
3 answers

Because static reserved in ES3. (no longer in ES5).

+4
source

Since static is a reserved word in Javascript before EcmaScript 5.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Reserved_Words

Some browsers may throw an error if it is used as a property of an object with the syntax object.word .

object['word'] make sure the error is not selected.

+3
source

static - reserved word (reserved for future use as a keyword) in javascript.

You can see the list of reserved words here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Reserved_Words

For example, x.in compiles to x["in"] , because in also a reserved word.

+1
source

Source: https://habr.com/ru/post/1486097/


All Articles