动态变量名称

编程入门 行业动态 更新时间:2024-10-24 01:57:34
本文介绍了动态变量名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

这可能是一个全新的问题,但我很难过。 如果我有这样的功能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

更多推荐

动态变量名称

本文发布于:2023-11-28 07:28:53,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1641476.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:变量   名称   动态

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!