I would like to know if there is a way to declare a local variable inside a sequence expression in Javascript. I want to declare the variable as a part of the sequence expression and not as a separate statement.
For example, I want to do something like this:
temp = "1", var a, ++i;
Thanks for the help guys!
Edit - I am trying to instrument Javascript (to find out potential DOM-based XSS)and the above code is just a snippet of the actual program. For example, I found a way to convert if statements to expressions using the ternary operator like:
if (a === 2) {a = 1} else {a = 3}
... is converted to a === 2 ? a = 1: a = 3;
? I wanted to know if var a = 2
can also be converted to an expression so that it can be added to a sequence expression.
As to why I am doing all this - I am replacing assignment statements in a JS program with a set of statements of my own. If I add in multiple statements in place of one single statement, it messes with the rest of the code. Therefore I am using a sequence expression to get around this.
For example, in a for loop like:
for (var i = 0; i < 2; i++) {}
I cant replace i = 0
with a bunch of semicolon separated statements. Thus I am trying to add in multiple statements with a sequence operator
I would like to know if there is a way to declare a local variable inside a sequence expression in Javascript. I want to declare the variable as a part of the sequence expression and not as a separate statement.
For example, I want to do something like this:
temp = "1", var a, ++i;
Thanks for the help guys!
Edit - I am trying to instrument Javascript (to find out potential DOM-based XSS)and the above code is just a snippet of the actual program. For example, I found a way to convert if statements to expressions using the ternary operator like:
if (a === 2) {a = 1} else {a = 3}
... is converted to a === 2 ? a = 1: a = 3;
? I wanted to know if var a = 2
can also be converted to an expression so that it can be added to a sequence expression.
As to why I am doing all this - I am replacing assignment statements in a JS program with a set of statements of my own. If I add in multiple statements in place of one single statement, it messes with the rest of the code. Therefore I am using a sequence expression to get around this.
For example, in a for loop like:
for (var i = 0; i < 2; i++) {}
I cant replace i = 0
with a bunch of semicolon separated statements. Thus I am trying to add in multiple statements with a sequence operator
- 2 My question is why would you want to do this? – elclanrs Commented Jun 25, 2013 at 6:12
-
1
temp = "1", var
does not make any sense... It will throw a syntax error, saying unexpected token – Sushanth -- Commented Jun 25, 2013 at 6:13 - what do mean??I dont see any purpose in your question. – user2412575 Commented Jun 25, 2013 at 6:16
- 1 @IcyFlame see my previous ment, I cant use multiple statements to replace i = 0 in for(i =0; i < 2; i++){} – everconfusedGuy Commented Jun 25, 2013 at 6:40
-
1
well, yeah.. then it is correct... but you should always declare variables before starting with the loop. Though, the declaration inside the initialisation part of the loop
for(var i = 0;...;...)
is allowed, I prefer to do it outside the loop. Keeps things neat in the loop and it is easier to understand the structure of the loop this way. – IcyFlame Commented Jun 25, 2013 at 6:53
5 Answers
Reset to default 3Indeed you must begin with the var
keyword followed by declarations and a semi-column. Then you can start your sequence.
If you really don't like it or do not want to use a semi-column (for whatever reason) you can try to make anonymous functions and use arguments as local variables :
(function(temp, a, i){ i=a, i++ })("1", 1)
So in a for loop you can do something like :
for (var i=function(){ /* any statement you want */; return 0 }(); i<10; i++) { /* ... */ }
Hope that helps.
Apperently you want the statement var a = this.b
to be changed into something like this:
var a = this.b;
var lhs = a;
var rhs = this.b;
if(rhs == 1){
};
lhs = rhs;
To achieve this, you can write a separate function:
function xy(){
var a = this.b;
var lhs = a;
var rhs = this.b;
if(rhs == 1){
};
lhs = rhs;
return a;
}
To make this work inside of a for-loop, in a single line, you can transform it into this:
for (var lhs, rhs, a = (function(){var a = this.b; lhs = a; rhs = this.b; if(rhs == 1){} lhs = rhs; return a;}).apply(this, []); a < 10; a++)
That should basically do it. The apply
method guarantees that this
inside the function is the same as this
outside the function.
var temp="1", a , i=0;
++i;
is a clean way to do it
I still don't feel like your question makes any sense but what is wrong with this:
var a, i = 1, temp = "1"
If doesn't make sense to increment a variable you only just declared, the value would always be 1 anyway if it where possible ( assuming a default value of 0 )
If you have to increment you could do this
var a, i = 1, temp = "1", i = i + 1
Still doesn't make a lot of sense tough...
Maybe you mean this?
var temp="1",a,i=i+1;
I think you cannot use 'var' in any other way syntactically.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742287190a4415531.html
评论列表(0条)