如何创建随机方法名称

发布于 2024-08-25 03:25:37 字数 428 浏览 3 评论 0原文

我计划使用 JSONP 调用外部 Web 服务,以避免我不想创建可能与调用页面发生冲突的全局函数。我认为创建一个随机函数名称并将其传递出去会起作用。像这样的事情:

<script src="www.foo.com/b?cb=d357534">

其中cb是回调函数名称,服务器会返回

d357534({my json data});

我想知道的是如何创建随机函数名称,我确信我可以使用eval 但这是最​​好的方法吗?

本质上,我想做的是:

var d + Math.floor(Math.random()*1000001) = function(){...   

I plan on using JSONP to call an external web service to get around the fact that I don't want to create a global function that could potentially conflict with the calling page. I thought that creating a random function name and passing it up would work. Something like this:

<script src="www.foo.com/b?cb=d357534">

where cb is the callback function name, the server would return

d357534({my json data});

What I want to know is how to create the random function name, I'm sure I could use eval but is this the best way to go about it?

Essentially, what I am trying to do is this:

var d + Math.floor(Math.random()*1000001) = function(){...   

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

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

发布评论

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

评论(3

蓝咒 2024-09-01 03:25:37

这应该做你想做的事。您需要将函数名称保存在某处,以便可以将其传递到服务器,但您可以在本地范围内执行此操作,以避免污染全局名称空间。

var functionName = 'd' + Math.floor(Math.random()*1000001);
window[functionName] = function() { ... }

This should do what you want. You need to save the function name somewhere so that you can pass it to the server, but you can do that inside of a local scope to avoid polluting your global namespace.

var functionName = 'd' + Math.floor(Math.random()*1000001);
window[functionName] = function() { ... }
澉约 2024-09-01 03:25:37

要创建一个随机命名的全局变量,您可以这样做:

window['randomvar' + Math.floor(Math.random()*1000001)] = function() { ... };

现在当然您遇到了记住某个地方的随机名称的问题。您也可以为该变量指定一个随机名称。然后你必须记住那个变量的名称,以便你可以查看它的值,然后知道如何找到你的函数。一段时间后,事情就会开始变得奇怪。

To make a randomly-named global variable you could do this:

window['randomvar' + Math.floor(Math.random()*1000001)] = function() { ... };

now of course you've got the problem of remembering the random name somewhere. You could make up a random name for that variable too. Then you'd have to remember the name of that variable, so that you could look at its value and then know how to find your function. After a while, things are going to start getting weird.

心碎无痕… 2024-09-01 03:25:37

为什么不只使用计数器并在每次需要新函数时递增它:

var name = "callback" + window.COUNTER++;
window[name] = function() { ... };

如果您想避免全局命名空间被太多引用弄乱,您可以(并且应该)将计数器和回调附加到单个全局对象

var JSONP = window.JSONP;
var name = "callback" + JSONP.COUNTER++;
JSONP[name] = function() { ... };

:在这种情况下,您可以像这样调用该方法:

JSONP.callback_12(json);

粗略地说,您必须首先初始化 JSONP 对象和 COUNTER 变量。

Why don't just use a counter and increment it each time you need a new function:

var name = "callback" + window.COUNTER++;
window[name] = function() { ... };

If you want to avoid littering the global namespace with too many references you could (and should) attach the counter and callbacks to a single global object:

var JSONP = window.JSONP;
var name = "callback" + JSONP.COUNTER++;
JSONP[name] = function() { ... };

In this case you could call the method like this:

JSONP.callback_12(json);

Of coarse you have to initialize the JSONPobject and the COUNTER variable first.

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