开发者

C# and Javascript Variable

开发者 https://www.devze.com 2023-03-15 04:51 出处:网络
I have a var开发者_StackOverflow社区iable that is like this: var dependentVar = \"\'<%: somevalue %>\', \'<%: somevalue2 %>\', \'<%: somevalue3 %>\'\";

I have a var开发者_StackOverflow社区iable that is like this:

var dependentVar = "'<%: somevalue %>', '<%: somevalue2 %>', '<%: somevalue3 %>'";

Which must be passed into a another variable

var newVar = new var.Function(dependentVar).

The function is like

function(somevalue, somevalue2, somevalue3)

The problem I am facing is that when the variable is passed - it passes like this

"'123123', '123123123', '123123123'"

and it errors due to the " ? How can I fix this ?


Well I wouldn't really do it this way, but you could do this:

var dependentVar = ['<%: somevalue %>', '<%: somevalue %>', '<%: somevalue %>'];

yourfunction.apply(null, dependentVar);

I guess it's not that bad. You could also do

yourfunction(dependentVar[0], dependentVar[1], dependentVar[2]);

The first way does have the advantage that it'd work for any number of values, assuming the function was prepared to deal with that. (If it were, and there were no other reason for the function to be written that way, it could be changed to operate on an array directly.)

edit — OK, if you need to call a function via an object property, you'd do this:

var tmp = new var();
var newVar = tmp.someFunction.apply(tmp, dependentVar);

The first argument to "apply" will make sure that "tmp" is the value of this when "someFunction" is called.


You can do this as three separate variables:

var dependentVar1 = '<%: somevalue %>';
var dependentVar2 = '<%: somevalue2 %>';
var dependentVar3 = '<%: somevalue3 %>';

And then pass those values around as you like.


var somevalue = '<%: somevalue %>';
var somevalue2 = '<%: somevalue2 %>';
var somevalue3 = '<%: somevalue3 %>';

or just make an array with it:

var myArray = ['<%: somevalue %>', '<%: somevalue2 %>', '<%: somevalue3 %>'];

Really, the best way is probably to just pass a JSON object back to your javascript asynchronously though.


function('<%: somevalue %>', '<%: somevalue2 %>', '<%: somevalue3 %>');


The way you wrote it,dependentvar is treated as one string.

Your function requires three values - I am assuming you are passing only one value to the function.

0

精彩评论

暂无评论...
验证码 换一张
取 消