令人困惑的谷歌闭包库API

发布于 2024-09-26 02:03:46 字数 90 浏览 0 评论 0原文

有人可以向我解释闭包如何以更用户友好的形式工作吗?它的帮助和文档确实让我无处可去。如何执行简单的任务,例如选择和修改 dom(例如,选择页面上的所有内容并隐藏它们)?

could someone explain to me how the Closure works in more user-friendly form? Its help and documentation leads me nowhere really. How do you perform a simple task such as selecting and modifying the dom (e.g. select all on page and hide them)?

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

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

发布评论

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

评论(2

黎歌 2024-10-03 02:03:46

请参阅 http://derekslager.com/blog/posts/2010/ 06/google-closure-introduction.ashx,比较 #4,

隐藏所有 div

<html>
<head>
<script src="http://closure-library.googlecode.com/svn/trunk/closure/goog/base.js" type="text/javascript"></script>
<script language="JavaScript">
  goog.require('goog.dom.query');
  goog.require('goog.style');
</script>
<script> 
  function HideElement(selector) {
    goog.array.map(goog.dom.query(selector, null), function(e) { 
        goog.style.showElement(e, false); 
    });
  }
</script>
</head>
<body>
   <div>div</div>
   <p>paragraph</p>
   <div>another div</div>
   <input type="button" value="hide" onclick="HideElement('div');"/>
</body>
</html>

不过,无法帮助您进行用户友好的细分。

See http://derekslager.com/blog/posts/2010/06/google-closure-introduction.ashx, Comparison #4,

Hide all div's:

<html>
<head>
<script src="http://closure-library.googlecode.com/svn/trunk/closure/goog/base.js" type="text/javascript"></script>
<script language="JavaScript">
  goog.require('goog.dom.query');
  goog.require('goog.style');
</script>
<script> 
  function HideElement(selector) {
    goog.array.map(goog.dom.query(selector, null), function(e) { 
        goog.style.showElement(e, false); 
    });
  }
</script>
</head>
<body>
   <div>div</div>
   <p>paragraph</p>
   <div>another div</div>
   <input type="button" value="hide" onclick="HideElement('div');"/>
</body>
</html>

Can't help you with the user-friendly breakdown, though.

你在看孤独的风景 2024-10-03 02:03:46

一开始我认为 API 文档真的很棒,但是在编写了几百行代码之后,我遇到了各种怪癖和问题。例如,dom 模块文档没有明确的入口点来发现 dom 操作方法 ->所有顶级链接都指向其内部使用的辅助对象。如果在包参考列表中单击 dom,然后单击 DomHelper,您可以找到一些有用的方法。看来您需要实例化 DomHelper 才能访问这些工具?

幸运的是,他们确实在整个 API 文档中包含了指向代码的便捷链接。如果您浏览 DomHelper 源代码,您会发现列出的大多数方法都可以直接从 goog.dom 命名空间获得!

我的另一个主要抱怨是文档通常不列出参数类型/名称/描述。例如,如果您展开 goog.dom.DomHelper.contains,它不会列出任何参数,但代码会正确注释两个参数。我不敢相信他们制作了如此详尽的注释和文档库,然后却未能将这些信息包含在(生成的)文档中!尽管在浏览他们的代码时,您也经常会在他们的注释中发现简洁且无信息的注释。

所以,总结一下:阅读代码!我总是讨厌听到这个答案,但这似乎是目前最好的选择。

我也有 O'Reilly Closure 书,虽然它确实提供了一些见解,但对于实际使用库中提供的基本模式和工具仍然不是很深入。我真的很想更好地了解图书馆的各个部分是如何交互的。我想有人应该制作一本闭包工具食谱?

I thought the API docs were really great at first, but after writing a few hundred lines of code I've run into all kinds of quirks and problems. For instance the dom module documentation doesn't have a clear entry point for discovering dom manipulation methods -> all the top-level links are to helper objects it uses internally. You can find some useful methods though if in the package reference list you click dom, then DomHelper. It seems like you need to instantiate a DomHelper to get access to those tools though?

Luckily they did include handy links into the code all over the API docs. If you poke around in the DomHelper source you'll see that most of the listed methods are available directly from the goog.dom namespace!

My other major gripe is that the docs often don't list argument types/names/descriptions. For instance if you expand goog.dom.DomHelper.contains it doesn't list any arguments, but the code correctly annotates two args. I can't believe they made such a thoroughly annotated and documented library and then failed to include that information in the (generated) docs! Although while browsing their code you will often find terse and uninformative comments in their annotations too.

So, to summarize: read the code! I always hate hearing that answer but it seems to be the best option right now.

I have the O'Reilly Closure book too, and although it does provide some insights it still isn't very in-depth on actually using basic patterns and tools provided in the library. I would really like a better over-view of how parts of the library are intended to interact. I guess someone should make a closure-tools cookbook?

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