Javascript:如何动态更改函数调用参数?
我从 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以做的是隐藏该函数,以便能够更改第三个参数。您应该在获取 JSON 之前定义该遮蔽函数。
然后你仍然可以
eval
它(这不是很安全,但如果我没有记错的话,这不是你的重点),它会调用影子函数。更通用的阴影方法如下:
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.
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:
Fiddle: http://jsfiddle.net/n7dLX/
换服务器。而不是返回
Return
Change the server. Rather than returning
Return
假设您无法更改从服务器发送的内容,您可以简单地使用正则表达式运行
innerText
并在插入之前更新 HTML。这可以通过执行以下操作来实现:
可以通过以下方式调用:
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.This can be functionized by doing the following:
This can be called in this way:
我不明白你在做什么,但一般来说,如果你不想依赖参数的顺序,则使函数采用一个参数,该参数是一个其属性是参数的对象:
在这种情况下,你应该使用 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:
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.