Javascript:如何动态更改函数调用参数?

发布于 2024-11-25 06:55:49 字数 852 浏览 1 评论 0原文

我从 jquery 的 json 调用接收到一些“正文”内容,我可以通过执行以下操作获取返回的唯一 javascript 元素:

script_element = $(data.body)[1]

这等于:

<script type=​"text/​javascript">​
    updater('foo', 'bar', {}, '0', constant='');
</script>​

因此, typeof script_element 返回 "object"< /code>

而且,如果我运行 script_element.innerText,我可以得到:

updater('foo', 'bar', {}, '0', constant='');

收到这个脚本后,我现在要做的就是对其运行 eval,但在我周围寻找无法找到运行 eval 更改函数调用参数的方法。

我想要做的是更改调用的 third 参数,在本例中为 {},它可以根据 json 调用的返回进行更改,因此我不能只搜索 {}

例如,我还可以执行 script_element.text.split(',')[2] ,并动态更改此文本,但我认为应该有更好的方法来执行此操作。

我不知道javascript是否可以识别和处理“未来的方法调用”,但仍然认为应该有更好的方法。

有什么想法吗?

I'm receiving some 'body' content from a jquery's json call, where I can get the unique javascript element returned by doing:

script_element = $(data.body)[1]

This equals to:

<script type=​"text/​javascript">​
    updater('foo', 'bar', {}, '0', constant='');
</script>​

So, typeof script_element returns "object"

And, if I run script_element.innerText, I can get:

updater('foo', 'bar', {}, '0', constant='');

After receiving this script, what I'm doing right now is just run an eval on it, but searching around I couldn't get a way to run eval changing function call params.

What I'm trying to do is change the third param of the call, in this case the {}, that can change depending on the return of the json call, so I can't just search for {}.

I could also do script_element.text.split(',')[2] for example, and change this text on the fly, but I was thinking there should be a better way to do this.

I don't know if javascript can recognize and treat a "future method call", but still think there should be a better way.

Any idea?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

笑看君怀她人 2024-12-02 06:55:49

您可以做的是隐藏该函数,以便能够更改第三个参数。您应该在获取 JSON 之前定义该遮蔽函数。

var originalUpdater = updater; // keep old function to call

// overwrite (shadowing)
updater = function(a, b, c, d, e) {
    // change c appropriately here
    originalUpdater(a, b, c, d, e);
}

然后你仍然可以eval它(这不是很安全,但如果我没有记错的话,这不是你的重点),它会调用影子函数。


更通用的阴影方法如下:

var originalUpdater = updater; // keep old function to call

// overwrite (shadowing)
updater = function() {
    // change arguments[2] appropriately here
    originalUpdater.apply(this, arguments);
}

Fiddle: http://jsfiddle.net/n7dLX/

What you could do is shadowing the function so as to be able to alter the third argument. You ought to define that shadowing function before fetching the JSON.

var originalUpdater = updater; // keep old function to call

// overwrite (shadowing)
updater = function(a, b, c, d, e) {
    // change c appropriately here
    originalUpdater(a, b, c, d, e);
}

Then you can still just eval it (which is not very safe, but that's not your point if I'm not mistaking), and it will call the shadow function.


A more generic shadowing method would be along the lines of:

var originalUpdater = updater; // keep old function to call

// overwrite (shadowing)
updater = function() {
    // change arguments[2] appropriately here
    originalUpdater.apply(this, arguments);
}

Fiddle: http://jsfiddle.net/n7dLX/

财迷小姐 2024-12-02 06:55:49

换服务器。而不是返回

<script type=​"text/​javascript">​
    updater('foo', 'bar', {}, '0', constant='');
</script>​

Return

{
  "method": "updater",
  "params": [
    "foo", "bar", {}, "0", ''
  ]
}

Change the server. Rather than returning

<script type=​"text/​javascript">​
    updater('foo', 'bar', {}, '0', constant='');
</script>​

Return

{
  "method": "updater",
  "params": [
    "foo", "bar", {}, "0", ''
  ]
}
忘你却要生生世世 2024-12-02 06:55:49

假设您无法更改从服务器发送的内容,您可以简单地使用正则表达式运行 innerText 并在插入之前更新 HTML。

var replacer = /\w+\(([^()]+)\)/gi;
script_element.innerText.replace(replacer, function(matched_text, func_params){
    var orig_func_params = func_params;
    // Make changes to func_params here.
    return matched_text.replace(orig_func_params, func_params);
});

这可以通过执行以下操作来实现:

var replacer = /\w+\(([^()]+)\)/gi;
function replace_arg(script_element, arg_index, replacement_value) {
    script_element.innerHTML = script_element.innerHTML.replace(replacer, 
    function(matched_text, func_params){
        var orig_func_params = func_params;
        func_params = func_params.split(",");
        if (arg_index >= func_params.length) {
             throw new RangeError(arg_index + " is out of range. Total args in function:" + func_params.length);
        }
        func_params[arg_index] = JSON.stringify(replacement_value);
        return matched_text.replace(orig_func_params, func_params.join(","));
    });
return script_element;
}

可以通过以下方式调用:

script_element = replace_arg(script_element, 3, {"new":"arg"});

Assuming that you cannot change what is being sent over from the server, you can simply run through the innerText with a regular expression and pass update the HTML before you insert it.

var replacer = /\w+\(([^()]+)\)/gi;
script_element.innerText.replace(replacer, function(matched_text, func_params){
    var orig_func_params = func_params;
    // Make changes to func_params here.
    return matched_text.replace(orig_func_params, func_params);
});

This can be functionized by doing the following:

var replacer = /\w+\(([^()]+)\)/gi;
function replace_arg(script_element, arg_index, replacement_value) {
    script_element.innerHTML = script_element.innerHTML.replace(replacer, 
    function(matched_text, func_params){
        var orig_func_params = func_params;
        func_params = func_params.split(",");
        if (arg_index >= func_params.length) {
             throw new RangeError(arg_index + " is out of range. Total args in function:" + func_params.length);
        }
        func_params[arg_index] = JSON.stringify(replacement_value);
        return matched_text.replace(orig_func_params, func_params.join(","));
    });
return script_element;
}

This can be called in this way:

script_element = replace_arg(script_element, 3, {"new":"arg"});
海夕 2024-12-02 06:55:49

我不明白你在做什么,但一般来说,如果你不想依赖参数的顺序,则使函数采用一个参数,该参数是一个其属性是参数的对象:

function add(params) {
    var a = params.hasOwnProperty("paramA") ? params.paramA : 0;
    var b = params.hasOwnProperty("paramB") ? params.paramB : 0;
    return a + b;
}

add({paramA: 1, paramB: 2});

在这种情况下,你应该使用 hasOwnProperty 来在尝试访问该函数之前,检查该函数是否已传递了您要查找的参数。

I don't understand what you are doing, but in general if you don't want to rely on the order of parameters make the function take one parameter that is an object whose properties are the parameters:

function add(params) {
    var a = params.hasOwnProperty("paramA") ? params.paramA : 0;
    var b = params.hasOwnProperty("paramB") ? params.paramB : 0;
    return a + b;
}

add({paramA: 1, paramB: 2});

In this case you should use hasOwnProperty to check if the function was passed the parameter you are looking for before trying to access it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文