如何将 javascript 函数锁定到本地范围?我想阻止它使用全局变量

发布于 2024-11-09 10:03:51 字数 173 浏览 0 评论 0原文

有没有办法阻止函数使用全局变量,例如 documentwindownavigator 和其他声明的全局函数?

EDIT1:如果我可以选择受限制的全局对象,那就太好了,因为我想允许该函数使用 Math 对象及其函数作为示例......

Is there a way to prevent a function from using global variables like document, window, navigator, and other declared global functions ?

EDIT1: And if I could choose which global objects that are restricted it would be great, because I would like to allow the function to use for an example the Math object and it's functions...

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

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

发布评论

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

评论(2

貪欢 2024-11-16 10:03:51

<块引用>

有没有办法阻止函数使用全局变量,如文档、窗口、导航器和其他声明的全局函数?

不,除非...

完成此任务的唯一方法是可以更改函数的词法范围——也就是说,修改源代码 以某种方式,例如如下所示包装它。

想象一下:

;(function () {
    var window = "Hello"

    // original content
    function foo () {
        alert(window)
    }
    foo()

})()

这种方法经常在库中用于创建私有名称空间,但在这些情况下,原始源也是可用的,并且在设计时就考虑到了这一点。我之前曾将其与 document 一起使用来更改本地版本的 jQuery。

虽然 with 乍一看可能看起来很有希望,但重要的是要认识到它也只是一个词法结构,并且不引入动态变量。

快乐编码。

Is there a way to prevent a function from using global variables like document, window, navigator, and other declared global functions?

No, unless...

The only way this task is possible is if the lexical scope of the functions can be altered -- that is, the source is modified in some way, such as wrapping it as shown below.

Imagine:

;(function () {
    var window = "Hello"

    // original content
    function foo () {
        alert(window)
    }
    foo()

})()

This approach is used often in libraries to create private namespaces but, in those cases, the original source is also available and designed with this in mind. I have used this with document before to alter a local version of jQuery.

While with might look promising at first, it is important to realize it is only a lexical construct as well and does not introduce dynamic variables.

Happy coding.

寻找我们的幸福 2024-11-16 10:03:51

您可以要求用户限制使用 ADsafe 代码。来自网站:

ADsafe 可以安全地将访客代码(例如第三方脚本广告或小部件)放置在网页上。 ADsafe 定义了一个 JavaScript 子集,该子集功能强大,足以允许访客代码执行有价值的交互,同时防止恶意或意外损坏或入侵。 ADsafe 子集可以通过 JSLint 等工具进行机械验证,因此无需人工检查即可检查来宾代码的安全性。 ADsafe 子集还强制执行良好的编码实践,增加了来宾代码正确运行的可能性。

如果您勾选右侧的框,JSLint 将验证代码是否符合 ADsafe,因此可以在您的网站上安全执行。

You could ask your users to restrict themselves to ADsafe code. From the website:

ADsafe makes it safe to put guest code (such as third party scripted advertising or widgets) on a web page. ADsafe defines a subset of JavaScript that is powerful enough to allow guest code to perform valuable interactions, while at the same time preventing malicious or accidental damage or intrusion. The ADsafe subset can be verified mechanically by tools like JSLint so that no human inspection is necessary to review guest code for safety. The ADsafe subset also enforces good coding practices, increasing the likelihood that guest code will run correctly.

If you tick the right box, JSLint will verify that code is ADsafe-compliant and therefore safe to execute on your site.

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