+ 6
Creating a variable
is it possible to let the script creating a new var?(while the script is running)
7 Antworten
+ 4
@jason I think you are talking about dynamic variable creation.
While your code is executing or in runtime you can use bracket syntax to create a dynamic variable. In javascript everything is a object. so use bracket syntax to create a new variable like this...
var a = 'some value';
window[a] = "anything";
or
objectname[a] = "any type";
You can access the variable like this..
window[a] and objectname[a]
+ 8
All JS variables are dynamic, meaning that they're created and deleted ( less or more ) at run time...
They are created just after parsing in order of instructions encountered. When a function call is encountered, the interpreter jump to the function body and create a new variable scope, where it'll store the variables declared inside the function ( and the implicite arguments variables ). When returning, the scope of function is deleted ( garbage collected ): so, in practice, it's not only << possible to let the script creating a new var >> at run time, it's almost the only way ^^
+ 6
and how?
+ 5
thnx :)
+ 1
Yes.
+ 1
Simply use var or let keyword inside the script.
var some = "new variable";
- 1
var a ="new variable"