JavaScript 别名

发布于 2024-07-25 07:25:10 字数 290 浏览 3 评论 0原文

我的目标是创建一种“Javascript 库”(如果你可以这样称呼它的话)。 我打算用它来在浏览网页时操作页面,将其动态加载为greasemonkey 脚本。 这个想法是将“win”映射到window、“doc”-> 文档,“win.loc” -> win.location 以及其他一些方便的映射,但您明白了。 您能否给我一些示例,让我从中获取语法,然后我将推断其余部分? 非常感谢。

my goal is to create a sort of "Javascript library," if you could call it that. I'm intending to use it to just manipulate pages as I browse the web, dynamically loading it as a greasemonkey script. The idea is to have "win" get mapped to window, "doc" -> document, "win.loc" -> win.location, and a few other convenience mappings, but you get the idea. Can you just give me a few examples for me to pick up the syntax from, and I'll extrapolate the rest? Thanks so much.

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

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

发布评论

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

评论(2

野生奥特曼 2024-08-01 07:25:10

只需像这样分配变量:

var win = window;
var doc = document;

在这种情况下,如果不修改 window 对象,就无法分配 win.loc 。 另外,窗口对象很特殊,因为在分配 win 后,您可以使用 win.winwin.win.win 获取它> 等等(window 是全局对象。)

在任何情况下,您仍然可以将 loc 分配给 window code> object:

win.loc = window.location;

// Can now be referenced as:
loc; // (window is the global object)
win.loc;
win.location;
window.location;

现在这就是如何执行您所要求的操作。 您很可能不应该这样做。 通常,框架被定义为仅使用一个具有合适名称的全局变量:

var Blixt = (function () {
    var localVariable = 123;

    return {
        loc: window.location,
        myFunc: function () {
            alert(localVariable);
        }
    };
})();

如果您不熟悉 JavaScript,那么所有这些都是相当复杂的东西,因此如果您是 JavaScript 新手,那么这不是一个好的项目。

Just assign variables like so:

var win = window;
var doc = document;

You can't assign win.loc without modifying the window object in this case though. Also, the window object is special because after assigning win, you'll be able to get it with win.win or win.win.win and so on (window is the global object.)

In any case, you can still assign loc to the window object:

win.loc = window.location;

// Can now be referenced as:
loc; // (window is the global object)
win.loc;
win.location;
window.location;

Now that is how to do what you're asking for. Most likely you shouldn't do this. Normally, frameworks are defined to use up only one global variable, with a suitable name:

var Blixt = (function () {
    var localVariable = 123;

    return {
        loc: window.location,
        myFunc: function () {
            alert(localVariable);
        }
    };
})();

All this is pretty complex stuff if you're not familiar with JavaScript though, so it's not a good project to start with if you're new to JavaScript.

晒暮凉 2024-08-01 07:25:10

**注意:**这实际上是对 Blixt 上面答案的澄清请求,该答案不适合一条评论。

好的,谢谢 - 这是一个很好的答案。 我只想对此进行一些澄清:

  • 如果我使用上面给出的语法定义别名,win.loc 的行为将完全window.location 相同,没有任何例外吗? (只是确保)

  • 我遵循了其中的大部分内容,但是带有 Blixt 函数的部分我不明白。 定义此函数的预期行为/结果是什么?

  • 有点 JS,但是为什么这不是一个好主意? 它可能会产生什么负面后果? 我只是想对通过 CTRL 键单击另一个选项卡中的链接打开的选项卡执行诸如 Cl javascript:win.loc=doc.ref 之类的操作,没什么太复杂的。< /p>

  • 说到这里,我有一种感觉,这可能因浏览器而异,但是有没有可能的方法让我定义一个 js: URI 方案并将其映射到 javascript: 一个? 感谢您的精彩回答。

**NOTE:**This is really a request for clarification of Blixt's answer above, which would not fit into one comment.

Ok, thanks - this is a pretty good answer. I'd just like to make some clarifications to it:

  • If I define the aliases using the syntax you've given above, win.loc will behave exactly the same as window.location without any exceptions? (Just making sure)

  • I followed most of it, but the part with the Blixt function I did not understand. What is the expected behavior/result of defining this function?

  • I am kinda new to JS, but why is this not a good idea? What negative consequences could it possibly have? I'm just looking to do things like C-l javascript:win.loc=doc.ref<ENTER> for a tab that was opened by CTRL-clicking a link in another tab, nothing too complex.

  • Speaking of which, and I have a feeling that this might be different from browser to browser, but is there any possible way for me define a js: URI scheme and map it to the javascript: one? Thanks for the great answer.

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