这可能是一个全新的问题,但我很难过。 如果我有这样的功能as: 函数DoSomething(strVarName){ ..... } 我称之为: DoSomething(''myVarName''); 在函数内部,怎么做我为名为myVarName的变量赋值了一个值 (即名称为strVarName值的变量)? 谢谢! Russ
Hi, This may be a totally newbie question, but I''m stumped. If I have a function such as: function DoSomething(strVarName) { ..... } and I call it like so: DoSomething(''myVarName''); Inside the function, how do I assign a value to the variable named myVarName (i.e. the variable whose name is the value of strVarName)? Thanks! Russ
推荐答案Russ Chinoy写道: Russ Chinoy wrote: function DoSomething(strVarName){ .... } 我称之为: DoSomething(''myVarName''); 里面该函数,如何为名为myVarName的变量赋值(即名称为strVarName的值的变量)? function DoSomething(strVarName) { .... } and I call it like so: DoSomething(''myVarName''); Inside the function, how do I assign a value to the variable named myVarName (i.e. the variable whose name is the value of strVarName)?
window [strVarName] =某个值; Mi ck
window[strVarName]=some value; Mick
Mick White写道: Mick White wrote: Russ Chinoy写道: Russ Chinoy wrote: function DoSomething(strVarName){ .... } 我称之为: DoSomething(''myVarName''); 在函数内部,我如何为名为 myVarName的变量赋值(即名称为strVarName的值的变量)? function DoSomething(strVarName) { .... } and I call it like so: DoSomething(''myVarName''); Inside the function, how do I assign a value to the variable named myVarName (i.e. the variable whose name is the value of strVarName)?
window [strVarName] =某个值;
window[strVarName]=some value;
不,不要'' T。 'window''可能是许多 案例中对全局对象的引用,但并非全部。改为使用对全局对象的引用: var MyVarName = ...; var _global = this; 函数doSomething(strVarName) { ... _global [strVarName] ... } doSomething(''myVarName''); 如果你担心新的全球`_global'会破坏你的全球 命名空间,你也可以这样做 var MyVarName = ...; 函数doSomething(strVarName) { ... doSomething._global [strVarName] ... } doSomething._global = this; doSomething(''myVarName''); PointedEars
No, don''t. `window'' may be a reference to the Global Object in many cases, but not in all. Use a reference to the Global Object instead: var MyVarName = ...; var _global = this; function doSomething(strVarName) { ... _global[strVarName] ... } doSomething(''myVarName''); If you are concerned that the new global `_global'' spoils your global namespace, you can instead also do var MyVarName = ...; function doSomething(strVarName) { ... doSomething._global[strVarName] ... } doSomething._global = this; doSomething(''myVarName''); PointedEars
Mick White写道: Mick White wrote: Russ Chinoy写道: Russ Chinoy wrote: 函数DoSomething(strVarName){ .... } 我称之为所以: DoSomething(''myVarName''); 在函数内部,如何为名为 myVarName的变量赋值(即名称为strVarName的值的变量)? function DoSomething(strVarName) { .... } and I call it like so: DoSomething(''myVarName''); Inside the function, how do I assign a value to the variable named myVarName (i.e. the variable whose name is the value of strVarName)?
window [strVarName] =某个值;
window[strVarName]=some value;
不,不要'' T。 'window''可能是许多 案例中对全局对象的引用,但并非全部。改为使用对全局对象的引用: var myVarName = ...; var _global = this; 函数doSomething(strVarName) { ... _global [strVarName] ... } doSomething(''myVarName''); 如果你担心新的全球`_global'会破坏你的全球 命名空间,你也可以这样做 var myVarName = ...; 函数doSomething(strVarName) { ... doSomething._global [strVarName] ... } doSomething._global = this; doSomething(''myVarName''); PointedEars
No, don''t. `window'' may be a reference to the Global Object in many cases, but not in all. Use a reference to the Global Object instead: var myVarName = ...; var _global = this; function doSomething(strVarName) { ... _global[strVarName] ... } doSomething(''myVarName''); If you are concerned that the new global `_global'' spoils your global namespace, you can instead also do var myVarName = ...; function doSomething(strVarName) { ... doSomething._global[strVarName] ... } doSomething._global = this; doSomething(''myVarName''); PointedEars
更多推荐
动态变量名称
发布评论