简体   繁体   中英

Bracket and dot notation in compiled CoffeeScript

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

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

It compiles to this:

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

By curiosity, I am wondering: why is the dot notation used for the first call and the bracket notation for the second call?

Because static is reserved in ES3. (not anymore in ES5).

Because static is a reserved word in Javascript prior to EcmaScript 5 .

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

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

object['word'] ensure no error will be thrown.

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

You can see a 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 is also a reserved word.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM