1
  1. 我在名为 validate 的函数中声明了一个名为 cont 的局部变量。
  2. 我正在从内部调用一个函数过程验证。
  3. 我将字符串“cont”作为验证函数的参数发送。
  4. 在使用字符串'cont'的过程函数中,我想访问javascript局部变量的值,如window ['cont']。但我不确定。
  5. 我想做的是尝试访问 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]]);

参数的内容是

"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>

然后 casablanca 声明 $GlOBALS 将不包含局部范围变量,这与 javascript 中的方式相同。这是正确的。

4

5 回答 5

2

Your code is correct, but what you're expecting is wrong. $GLOBALS in PHP does not include local variables, and the same thing applies to window in JavaScript. cont is local to validate, so obviously it can't be accessed from process.

In PHP, you need to explicitly declare a variable as global inside a function. In JavaScript, it works the other way round: any variables declared using var are local, anything that is not declared is global.

于 2010-10-22T05:13:46.423 回答
2

http://www.i-marco.nl/weblog/archive/2007/06/14/variable_variables_in_javascri - 发现,这可能对你有帮助,或者

https://stackoverflow.com/questions/592630/javascript-variable-variables

于 2010-10-21T11:51:18.083 回答
2

正如其他人所建议的那样,您将无法在代码的其他地方访问本地范围内的变量。

您最初发布的代码是:

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();

所有这一切都是创建一个全局地图,您可以使用字符串添加和索引。

于 2010-10-22T07:07:25.883 回答
1
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)});
于 2010-10-21T11:56:37.207 回答
0

我试图理解为什么需要将变量作为字符串传递以及为什么要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();

仅供参考:声明全局变量通常被认为是不好的做法,尤其是在您似乎不需要它们的情况下。(不过,我可能错了;我不完全确定您要完成什么)

编辑:我建议使用一些函数并传递一些变量,而不是使用eval()-esque 变量引用。例如,您可以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函数。

于 2010-10-21T13:53:00.373 回答