JavaScript 别名
我的目标是创建一种“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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
只需像这样分配变量:
在这种情况下,如果不修改
window
对象,就无法分配win.loc
。 另外,窗口对象很特殊,因为在分配win
后,您可以使用win.win
或win.win.win
获取它> 等等(window
是全局对象。)在任何情况下,您仍然可以将
loc
分配给window code> object:
现在这就是如何执行您所要求的操作。 您很可能不应该这样做。 通常,框架被定义为仅使用一个具有合适名称的全局变量:
如果您不熟悉 JavaScript,那么所有这些都是相当复杂的东西,因此如果您是 JavaScript 新手,那么这不是一个好的项目。
Just assign variables like so:
You can't assign
win.loc
without modifying thewindow
object in this case though. Also, the window object is special because after assigningwin
, you'll be able to get it withwin.win
orwin.win.win
and so on (window
is the global object.)In any case, you can still assign
loc
to thewindow
object: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:
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.
**注意:**这实际上是对 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 aswindow.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 thejavascript:
one? Thanks for the great answer.