简体   繁体   中英

What is this kind of variable in javascript?

I am trying to decipher a javascript and I found this kind of variable. Could someone please tell me how it works? Before judging me for asking such a stupid question instead of googling it I'd like to point out that googling something you don't even know of is quite hard. I tried javascript variable types but no luck. :)

if (! this.sh_languages) {
  this.sh_languages = {};
}
sh_languages['tcl'] = [
  [
    [
      /#/g,
      'sh_comment',
      1
    ],
    [
      /\b[+-]?(?:(?:0x[A-Fa-f0-9]+)|(?:(?:[\d]*\.)?[\d]+(?:[eE][+-]?[\d]+)?))u?(?:(?:int(?:8|16|32|64))|L)?\b/g,
      'sh_number',
      -1
    ],
    [
      /"/g,
      'sh_string',
      2
    ],
    [
      /'/g,
      'sh_string',
      3
    ],
    [
      /~|!|%|\^|\*|\(|\)|-|\+|=|\[|\]|\\|:|;|,|\.|\/|\?|&|<|>|\|/g,
      'sh_symbol',
      -1
    ],
    [
      /\{|\}/g,
      'sh_cbracket',
      -1
    ],
    [
      /\b(?:proc|global|upvar|if|then|else|elseif|for|foreach|break|continue|while|set|eval|case|in|switch|default|exit|error|proc|return|uplevel|loop|for_array_keys|for_recursive_glob|for_file|unwind_protect|expr|catch|namespace|rename|variable|method|itcl_class|public|protected|append|binary|format|re_syntax|regexp|regsub|scan|string|subst|concat|join|lappend|lindex|list|llength|lrange|lreplace|lsearch|lset|lsort|split|expr|incr|close|eof|fblocked|fconfigure|fcopy|file|fileevent|flush|gets|open|puts|read|seek|socket|tell|load|loadTk|package|pgk::create|pgk_mkIndex|source|bgerror|history|info|interp|memory|unknown|enconding|http|msgcat|cd|clock|exec|exit|glob|pid|pwd|time|dde|registry|resource)\b/g,
      'sh_keyword',
      -1
    ],
    [
      /\$[A-Za-z0-9_]+/g,
      'sh_variable',
      -1
    ]
  ],
  [
    [
      /$/g,
      null,
      -2
    ]
  ],
  [
    [
      /"/g,
      'sh_string',
      -2
    ],
    [
      /\\./g,
      'sh_specialchar',
      -1
    ]
  ],
  [
    [
      /'/g,
      'sh_string',
      -2
    ],
    [
      /\\./g,
      'sh_specialchar',
      -1
    ]
  ]
];

At first I thought it was a dictionary, but it did not seem that way.

Chrome throws this error when trying to declare it:

Uncaught SyntaxError: Unexpected token [

As the error message points out, it isn't any kind of variable. It is a syntax error.

Let's break it down:

var sh_languages declares a variable called sh_languages .

sh_languages['tcl'] accesses a property named tcl of an object stored in a variable named sh_languages .

You can't declare a variable and access a property of an object in that variable in the same statement.

[ starts an array literal.

[[ starts an array literal, then starts another array literal and puts the second array in the first index of the first array.

[[[ and a third array as the first index of the second array. It's turtles all the way down.

/#/g is a regular expression literal. It matches # characters and has the global flag. The regular expression would be the first index of the third array.

, the comma separates the first index from the second index.

var sh_languages['tcl'] = .. is invalid. It must be a plain identifier that follows var , not an expression (even that of an Reference Sepcification Type).

A correction, keeping in line with the form used, would be:

var sh_languages = {};
sh_languages['tcl'] = ..;

Or alternatively, the key could be written as part of the object literal itself:

var sh_languages = {
   tcl: ..
};

The .. stuff, as whatever is in the original, is an expression formed by various literals (array, string, regular expression, etc.) In this case that is some form of tree (nested arrays) with a prescribed format; the usage of the data therein depends on the semantic meaning.

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