提问人:Jayapal Chandran 提问时间:10/21/2010 最后编辑:BoltClockJayapal Chandran 更新时间:10/22/2010 访问量:1568
JavaScript 相当于 PHP $$ 美元
javascript equivalent of php $$ dollar dollar
问:
- 我在一个名为 validate 的函数中声明了一个名为 cont 的局部变量。
- 我正在从内部调用函数进程 validate。
- 我正在发送字符串“cont”作为参数来验证函数。
- 在使用字符串“cont”的进程函数中,我想访问javascript局部变量的值,例如window['cont']。但是我没有定义。
- 我正在尝试做的是尝试访问 php 中的 $GLOBALS 或 $$ 等变量。
这是我所做的事情的一个例子。
<script>
function process(str)
{
alert(window[str]);
}
function validate()
{
var cont='once there lived a king named midas';
process('cont')
}
validate();
</script>
原因是我把大部分形式都做成ajax。我不想制作这样的请求字符串。
var param = "command=insert&content=" + encodeURIComponent(cont);
我想这样做。
var param = makeParam('command,[insert],content,(cont)');
我在 makeparam 中所做的是使用正则表达式来提取键值对。 所以我从 (cont) 中获取字符串 cont,并将其代入窗口变量,如 window[cont]。cont 将具有字符串“cont”。
那么我们如何通过使用变量的名称作为字符串来获取变量的内容呢?
所以我正在寻找相当于 PHP 的 $$ 的 JavaScript
编辑
我提取 cont 的代码的一部分,它在 (cont) 中,这意味着我想要 () 之间的字符串内容。
nxt = str[i+1].match(/\((.*)\)$/)
if(nxt)param += '=' + encodeURIComponent(window[nxt[1]]);
param 的内容将是
"command=insert&content=once there lived a king"
// assume that once there lived a king is encoded
编辑。 注2.
在几个响应之后,我正在编辑代码以添加它。
我正在尝试像 php 中的$GLOBALS一样。
我还没有尝试过$GLOBALS是否也可以保留局部变量。
并了解到本地范围不会进入$GLOBALS。
阅读 Felix King 的更新后更新。
我想使用一个函数来构造一个尽可能简单的查询字符串。如下所示。
var param = makeParam('command,insert,/title/,/keywords/,/description/,mode,[1],fckcontent,(cont)');
// if it is a text without // or () then the it is a straight key value pair. so i will do comment=insert.
//if it is /title/ then the key is title and its value is an input elements value with id as title so title=getElementById('title')
//if it is mode,[1] then mode is the key and 1 is its direct value//
//if it is fckcontent,(cont) then fckcontent is the key and cont is a javascript local variable which will contain html content from a WYSIWYG editor.
// a sample result will be
var param = "command=insert&keywords=somekeywords&description=somedescription&mode=1&fckcontent=<p>once there lived a king<p>
然后卡萨布兰卡说$GlOBALS不会包含局部作用域变量,这在 JavaScript 中也是如此。没错。
答:
http://www.i-marco.nl/weblog/archive/2007/06/14/variable_variables_in_javascri - 发现,这可能对您有所帮助,或者
https://stackoverflow.com/questions/592630/javascript-variable-variables
评论
function validate()
{
var cont='once there lived a king named midas';
process('cont')
}
cont
在函数的局部作用域中定义,而不是在全局作用域中定义。要么只做
cont='once there lived a king named midas';
(不带 ) 或var
window.cont='once there lived a king named midas';
更新:
但是,为什么要在解析字符串时遇到这么多麻烦呢?你为什么不这样做:
var param = makeParam({command: 'insert', content: encodeURIComponent(cont)});
评论
我试图理解为什么您需要将变量作为字符串传递,以及为什么要通过对象访问。这看起来像您期望的代码执行以下操作:cont
window
process = function (str) {
alert(str); // This will alert 'once there lived a king named midas'
}
validate = function () {
var cont = 'once there lived a king named midas';
process(cont);
}
validate();
仅供参考:声明全局变量通常被认为是不好的做法,尤其是在这样的情况下,您似乎不需要它们。(不过,我可能错了;我不完全确定你想完成什么)
编辑:我建议使用一些函数并传递一些变量,而不是用-esque变量引用来搞砸。例如,您可以像这样实现:eval()
makeParam
var cont = 'Boggis, Bunce, and Bean.',
makeParam = function(str) {
var param = '',
i = 0,
arg = str.split(','),
l = arg.length;
while (i < l) {
if (arg[i + 1].match(/\([a-zA-Z]+\)/)) { // No reason to capture
param += [arg[1], cont].join('=');
i += 2;
} else if (arg[i].match(/\/[a-zA-Z]+\//)) { // No reason to capture
param += [
arg[i],
document.getElementById(arg[i]).value
].join('=');
i += 1;
} else {
param += [arg[i], arg[i + 1]].join('=');
i += 2;
}
param += (i + 1 === l) ? '' : '&';
}
return param;
};
param = makeParam('command,insert,/title/,/keywords/,/description/,mode,[1],fckcontent, (cont)');
param === 'command=insert&\
keywords=somekeywords&\
description=somedescription&\
mode=1&\
fckcontent=Boggis, Bunce, and Bean.';
但你可能想传入你的函数。cont
makeParam
评论
你的代码是正确的,但你期望的是错误的。 在 PHP 中不包括局部变量,同样的事情也适用于 JavaScript。 是本地的,所以显然不能从 访问。$GLOBALS
window
cont
validate
process
在 PHP 中,您需要在函数中显式声明变量为 global。在 JavaScript 中,它的工作方式正好相反:任何声明的变量都是局部的,任何未声明的变量都是全局的。var
评论
globals = {};
globals['cont']
cont
正如其他人所建议的那样,您将无法访问代码中其他地方的本地范围内的变量。
您最初发布的代码是:
function process(str) {
alert(window[str]); // This will alert 'once there lived a king named midas'
}
function validate() {
var cont = 'once there lived a king named midas';
process('cont');
}
validate();
另一种选择(而不是将所有内容都放在“窗口”变量映射中)是创建自己的映射。
因此,您的代码将变为:
var variables = { }
function process(str) {
alert(variables[str]); // This will alert 'once there lived a king named midas'
}
function validate() {
variables['cont'] = 'once there lived a king named midas';
process('cont');
}
validate();
所有这一切都是创建一个全局映射,您可以使用字符串添加和索引该映射。
下一个:动态修改静态变量变量数组
评论