0

I'm trying to set global variable 'table' in chrome console and the result is :

var table=5;
table
function table(data, [columns]) { [Command Line API] } 

'table' isn't a reserved variable so why I cant set it to something else? Thanks.

Roni Hacohen
  • 313
  • 5
  • 16

3 Answers3

1

See this question about global variables

you could try window.table or this.table if you truly need a global variable.

if you set window.table = 5; and then echo table in the console you will see the value that you set to window.table.

Community
  • 1
  • 1
j-u-s-t-i-n
  • 1,402
  • 1
  • 10
  • 16
1

It's a global because Chrome uses __commandLineAPI as the global object in the console.

It looks something like this:

with (typeof __commandLineAPI !== 'undefined' ? __commandLineAPI : { __proto__: null }) {
   // code executed in the console goes in here.
}

If you'd like a list of the functions in that object, you can run Object.keys(__commandLineAPI) and it will output this:

["$$", "$x", "dir", "dirxml", "keys", "values", "profile", "profileEnd", "monitorEvents", "unmonitorEvents", "inspect", "copy", "clear", "getEventListeners", "debug", "undebug", "monitor", "unmonitor", "table", "$0", "$1", "$2", "$3", "$4", "$_"]

I guess you can wrap it in a closure if you really need to:

(function() {
   var table = 1234;
   console.log(table);
})()

Or if you want to just overwrite it in the window, just do window.table = ...

0

Just add a prefix: var app_table = 5;

Emmanuel Lozoya
  • 128
  • 2
  • 10