Getting All Variables In Scope
No. "In scope" variables are determined by the "scope chain", which is not accessible programmatically.
For detail (quite a lot of it), check out the ECMAScript (JavaScript) specification. Here's a link to the official page where you can download the canonical spec (a PDF), and here's one to the official, linkable HTML version.
Update based on your comment to Camsoft
The variables in scope for your event function are determined by where you define your event function, not how they call it. But, you may find useful information about what's available to your function via this
and arguments by doing something along the lines of what KennyTM pointed out (for (var propName in ____)
) since that will tell you what's available on various objects provided to you (this
and arguments; if you're not sure what arguments they give you, you can find out via the arguments
variable that's implicitly defined for every function).
So in addition to whatever's in-scope because of where you define your function, you can find out what else is available by other means by doing:
var n, arg, name;
alert("typeof this = " + typeof this);
for (name in this) {
alert("this[" + name + "]=" + this[name]);
}
for (n = 0; n < arguments.length; ++n) {
arg = arguments[n];
alert("typeof arguments[" + n + "] = " + typeof arg);
for (name in arg) {
alert("arguments[" + n + "][" + name + "]=" + arg[name]);
}
}
(You can expand on that to get more useful information.)
Instead of that, though, I'd probably use a debugger like Chrome's dev tools (even if you don't normally use Chrome for development) or Firebug (even if you don't normally use Firefox for development), or Dragonfly on Opera, or "F12 Developer Tools" on IE. And read through whatever JavaScript files they provide you. And beat them over the head for proper docs. :-)
How to get list of all variables defined in the current scope?
Yes and no. "No" in almost every situation. "Yes," but only in a limited manner, if you want to check the global scope. Take the following example:
var a = 1, b = 2, c = 3;
for ( var i in window ) {
console.log(i, typeof window[i], window[i]);
}
Which outputs, amongst 150+ other things, the following:
getInterface function getInterface()
i string i // <- there it is!
c number 3
b number 2
a number 1 // <- and another
_firebug object Object firebug=1.4.5 element=div#_firebugConsole
"Firebug command line does not support '$0'"
"Firebug command line does not support '$1'"
_FirebugCommandLine object Object
hasDuplicate boolean false
So it is possible to list some variables in the current scope, but it is not reliable, succinct, efficient, or easily accessible.
Tensorflow get all variables in scope
I think you want tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES, scope='my_scope'). This will get all variables in a scope.
To pass to an optimizer you do not want all variables you would just want the trainable variables. Those are also kept in a default collection, which is tf.GraphKeys.TRAINABLE_VARIABLES
.
How to get all variables available in scope?
var_outer
truly ISN'T available in inner()
. Making a variable available in a nested function requires that the variable be stored in a different way from the start; Python did not do so, due to lack of any reference to the variable from a nested function. If you did insert such a reference (such as the bare statement var_outer
), it would show up in the output of locals()
.
Get a dict of all variables currently in scope and their values
Best way to merge two dicts as you're doing (with locals overriding globals) is dict(globals(), **locals())
.
What the approach of merging globals and locals is missing is (a) builtins (I imagine that's deliberate, i.e. you don't think of builtins as "variables"... but, they COULD be, if you so choose!-), and (b) if you're in a nested function, any variables that are local to enclosing functions (no really good way to get a dict with all of those, plus -- only those explicitly accessed in the nested function, i.e. "free variables" thereof, survive as cells in a closure, anyway).
I imagine these issues are no big deal for your intended use, but you did mention "corner cases";-). If you need to cover them, there are ways to get the built-ins (that's easy) and (not so easy) all the cells (variables from enclosing functions that you explicitly mention in the nested function -- thefunction.func_code.co_freevars
to get the names, thefunction.func_closure
to get the cells, cell_contents
on each cell to get its value). (But, remember, those will only be variables from enclosing functions that are explicitly accessed in your nested function's code!).
Getting all variables declared in a scope - C++
If one needs to iterate over a bunch of objects of the same type the typical data structure one uses is an array (old school) or a collection of some kind, depending on secondary requirements like uniqueness, sortedness etc. (new school). This would work with arrays of built-in types as well as with arrays of structs.
get all defined variables in current scope
For the current scope, it's not possible (AFAIK), for the global scope you have the global
object in node.js (window
in navigator embedded js engines), which get the global variables properties:
// on global scope, with x not defined as a "var"
x=25
console.info(global['x']===x) // --> writes down 'true'
Related Topics
Why Does Google Prepend While(1); to Their JSON Responses
Change Url Parameters and Specify Defaults Using JavaScript
Addeventlistener Using for Loop and Passing Values
How to Sort Array Inside Collection Record in Mongodb
Is There Any Non-Eval Way to Create a Function with a Runtime-Determined Name
What Is the Reason JavaScript Settimeout Is So Inaccurate
Nested Routes with React Router V4/V5
Angularjs Passing Data to $Http.Get Request
JavaScript Global Event Mechanism
How Is Almost Everything in JavaScript an Object
What Is the Purpose of a Plus Symbol Before a Variable
How to Render an Array of Objects in React
Preserving a Reference to "This" in JavaScript Prototype Functions
How to Deal with Big Numbers in JavaScript
I Know That Callback Function Runs Asynchronously, But Why
Navigator.Geolocation.Getcurrentposition Sometimes Works Sometimes Doesn'T