美元符号和美元符号的含义包含 HTML 脚本标记之外的 Javascript 块的大括号

发布于 2024-12-23 13:15:08 字数 1251 浏览 6 评论 0原文

我目前正在阅读“Javascript Web 应用程序”(O'Reilly,Alex MacCaw),很早就有一个代码片段可能看起来在 HTML 文档中执行 JS 函数,但它是未包含在

// template.html
<div>
  <script>
    function doSomething(aParam) {
      /* do stuff ... */
    };
  </script>
  ${ doSomething(this.someX) }
</div>

请有人解释一下 dollar-sign-curly-brace notation 吗?我目前正在学习 JS,之前没有见过这个,所以我推测它要么是代码执行的 JS 简写(如果是这样,为什么不终止分号?),或者可能是一些专有的模板标记(Ruby?PHP?),或者完全是其他东西?

非常感谢。

更新

事实证明,稍后在(上述书籍的)第 5 章中,我们将介绍 Javascript 模板。作者似乎假设读者在阅读本书之前已经遇到过这种模板技术。正如 Stackoverflow 成员 Esailija 所指出的,本书不是一本 Javascript 初学者指南;我应该补充一点,我正在阅读这本书,同时阅读'Javascript:The Good Parts'(O'Reilly,Douglas Crockford)等书。

我对某种模板有半点怀疑,但我没有考虑过纯 JS 模板。我过去使用过 PHP 和 RoR 框架,它们也使用类似的模板概念将模型数据注入到视图中。

最后一点:我阅读 'Javascript Web 应用程序' 的原因它讨论了 JS 领域内的模型-视图-控制器 (MVC) 模式。虽然它提倡使用 jQuery 库(以及其他库)来增强和加速开发,但它还不是一本jQuery API 书 -另一个新的javascript包装器;相反,本书利用此类库(在适当的情况下)来处理跨浏览器的 JS 不一致问题,同时让大家了解创建“Web 应用程序”时实际的 JS 最佳实践和模式。

I'm currently reading 'Javascript Web Applications' (O'Reilly, Alex MacCaw) and very early on there's a code snippet which might appear to execute a JS function, within an HTML document, yet it is not enclosed by <script> tags:

// template.html
<div>
  <script>
    function doSomething(aParam) {
      /* do stuff ... */
    };
  </script>
  ${ doSomething(this.someX) }
</div>

Please could someone kindly explain the dollar-sign-curly-brace notation? I'm currently learning JS and I haven't seen this before, so I'm presuming it's either JS shorthand for code execution (if so, why no terminating semi-colon?), or perhaps some proprietary templating mark-up (Ruby? PHP?), or something else entirely?

Many thanks.

UPDATE

It transpires that later on in Chapter 5 (of the aforementioned book) we are then introduced to Javascript templating. It appears to be an assumption by the author that the reader has already encountered this templating technique before reading the book. As noted by Stackoverflow member Esailija, this book is not a beginners' guide to Javascript; I should add that I'm reading this book in parallel with 'Javascript: The Good Parts' (O'Reilly, Douglas Crockford) amongst others.

I had half-suspected some kind of templating, but I hadn't considered pure JS templating. I have used PHP and RoR frameworks in the past which also used similar templating concepts for injecting model data into views.

One final point: my reason(s) for reading 'Javascript Web Applications' is that it discusses the Model-View-Controller (MVC) pattern within the JS sphere. While it advocates the use of the jQuery library (among others) to both enhance and accelerate development, it is not a jQuery API book or yet-another-new-javascript-wrapper; rather, the book makes use of such libraries (where appropriate) to deal with cross-browser JS inconsistencies, while driving home actual JS best practices and patterns in creating 'web applications'.

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

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

发布评论

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

评论(1

彡翼 2024-12-30 13:15:08

它不应该是 javascript,而是一个模板文件。模板文件包含静态 html 和非常愚蠢的表示逻辑,通常采用伪语言,就像在本例中一样。它不应该包含任何真正的 javascript - 正如书中所说,这是你不应该做的事情。

然后这本书进入了一个重构版本,只有静态 html 和伪语言语句。请参阅 Javascript 模板

这本书似乎是针对那些已经精通 javascript 并正在研究更好地构建 js 的人。

It's not supposed to be javascript but a template file. Template file contains static html and very dumb presentation logic, usually in a pseudolanguage as it is in this case. It should not contain any real javascript - as it is said in the book that it's something you shouldn't do.

The book then goes into a refactored version that only has static html and the pseudolanguage statement. See Javascript templating.

The book seems to be aimed at those who are already proficient at javascript and are looking into structuring their js better.

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