相当于php$$的javascript 我在一个名为validate的函数中声明了一个名为cont的局部变量 我正在从内部验证调用一个函数进程 我将字符串“cont”作为参数发送给验证函数 在使用字符串“cont”的process函数中,我想访问javascript局部变量的值,比如window['cont']。但我没有定义 我要做的是尝试访问php中的$GLOBALS或$$等变量

相当于php$$的javascript 我在一个名为validate的函数中声明了一个名为cont的局部变量 我正在从内部验证调用一个函数进程 我将字符串“cont”作为参数发送给验证函数 在使用字符串“cont”的process函数中,我想访问javascript局部变量的值,比如window['cont']。但我没有定义 我要做的是尝试访问php中的$GLOBALS或$$等变量,php,javascript,variable-variables,Php,Javascript,Variable Variables,这是我所做的一个例子 <script> function process(str) { alert(window[str]); } function validate() { var cont='once there lived a king named midas'; process('cont') } validate(); </script> 我想这样做 var param = makeParam('command,[insert],co

这是我所做的一个例子

<script>

function process(str)
{
   alert(window[str]);
}

function validate()
{
   var cont='once there lived a king named midas';
   process('cont')
}

validate();

</script>
我想这样做

var param = makeParam('command,[insert],content,(cont)');
我在makeparam中所做的是使用正则表达式来提取键值对。 所以我从(cont)中得到字符串cont,并将其替换为window变量,如window[cont]。cont将具有字符串“cont”

那么,我们如何使用变量名作为字符串来获取变量的内容呢

因此,我正在寻找与php等价的javascript$$

已编辑

代码的一部分,我在其中提取cont,cont位于(cont)内部,这意味着我希望字符串的内容在()之间

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>
var-param=makeParam('command,insert,/title/,/keywords/,/description/,mode,[1],fckcontent,(cont');
//如果它是一个没有//或()的文本,那么它是一个直接的键值对。因此,我将执行comment=insert。
//如果是/title/则键是title,其值是id为title的输入元素值,因此title=getElementById('title'))
//如果是mode,[1],则mode是键,1是其直接值//
//如果是fckcontent,(cont),那么fckcontent是键,cont是一个javascript局部变量,它将包含所见即所得编辑器中的html内容。
//将提供一个示例结果
var param=“command=insert&keywords=somekeywords&description=somedescription&mode=1&fckcontent=曾经有一个国王
然后卡萨布兰卡声明$GlOBALS将不包含局部范围变量,这在javascript中也是如此。没错。

-发现了,这可能会对您有所帮助,或者

-发现,这可能对您有所帮助,或者

cont
是在函数的局部作用域中定义的,而不是在全局作用域中定义的

cont='once there lived a king named midas';
(无
var
)或

更新:

但是为什么要在解析字符串时遇到这么多麻烦呢?为什么不:

var param = makeParam({command: 'insert', content: encodeURIComponent(cont)});
cont
是在函数的局部作用域中定义的,而不是在全局作用域中定义的

cont='once there lived a king named midas';
(无
var
)或

更新:

但是为什么要在解析字符串时遇到这么多麻烦呢?为什么不:

var param = makeParam({command: 'insert', content: encodeURIComponent(cont)});

我试图理解为什么需要将变量作为字符串传递,以及为什么要通过
窗口
对象访问
cont
。这就是您希望代码执行的操作:

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();
仅供参考:声明全局变量通常被认为是一种不好的做法,尤其是在这样的情况下,您似乎不需要它们。(不过,我可能错了;我不完全确定您想要实现什么)

Edit:我建议使用一些函数并传递一些变量,而不是乱搞
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.';
var cont='Boggis、Bunce和Bean',
makeParam=函数(str){
变量参数=“”,
i=0,
arg=str.split(','),
l=arg.长度;
而(i

但是您可能希望将
cont
传递到
makeParam
函数中。

我试图理解为什么需要将变量作为字符串传递,以及为什么要通过
窗口
对象访问
cont
。这看起来像是您希望代码执行的操作:

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();
仅供参考:声明全局变量通常被认为是一种不好的做法,尤其是在这样的情况下,您似乎不需要它们。(不过,我可能错了;我不完全确定您想要实现什么)

Edit:我建议使用一些函数并传递一些变量,而不是乱搞
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.';
var cont='Boggis、Bunce和Bean',
makeParam=函数(str){
变量参数=“”,
i=0,
arg=str.split(','),
l=arg.长度;
而(ifunction 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();