简体   繁体   English

C#和Javascript变量

[英]C# and Javascript Variable

I have a variable that is like this: 我有一个像这样的变量:

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

Which must be passed into a another variable 必须将其传递给另一个变量

var newVar = new var.Function(dependentVar) . 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. 第一个参数,以“应用”将确保“TMP”是价值this时候“someFunction”之称。

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. 确实,最好的方法可能只是将JSON对象异步传递回JavaScript。

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

The way you wrote it,dependentvar is treated as one string. 您编写它的方式,dependentvar被视为一个字符串。

Your function requires three values - I am assuming you are passing only one value to the function. 您的函数需要三个值-我假设您仅将一个值传递给该函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM