美元符号的含义包含JavaScript脚本以外的HTML脚本标记的花括号 [英] Meaning of Dollar Sign & Curly Braces Containing Javascript Block Outside of HTML Script Tag

查看:515
本文介绍了美元符号的含义包含JavaScript脚本以外的HTML脚本标记的花括号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在阅读'Javascript Web应用程序'(O'Reilly,Alex MacCaw),并且很早就有一段代码片段可能在HTML文档中执行JS函数,但它没有被< script> 标签所包围:

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

请有人解释美元符号大括号表示法 ?我正在学习JS,之前我没有看到过,所以我假设它是JS代码执行的简写(如果是这样,为什么不终止分号?),或者可能是一些专有的模板



非常感谢。

UPDATE



后来在第5章(上述书)中介绍了 Javascript模板。作者认为读者在阅读本书之前已经遇到了这种模板技术,这似乎是一个假设。正如 Stackoverflow成员Esailija 所指出的,本书是 Javascript初学者指南;我应该补充一点,我正在阅读与'Javascript:The Good Parts'(O'Reilly,Douglas Crockford)并行的这本书。



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



最后一点:我的理由)用于阅读'Javascript Web应用程序',它讨论了JS球体内的模型 - 视图 - 控制器(MVC)模式。虽然它倡导使用 jQuery库(除其他外)来增强和加速开发,但它不是 jQuery API书籍 或尚未-another全新的JavaScript,包装;相反,本书利用这些库(在适当的情况下)来处理跨浏览器的JS不一致性,同时推动家庭实际的JS最佳实践和创建'web应用程序'模式。

解决方案

它不应该是JavaScript,而是一个模板文件。模板文件包含静态html和非常哑的表示逻辑,通常以伪语言,因为它在这种情况下。它不应该包含任何真正的JavaScript - 正如它在书中所说,这是你不应该做的。



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



这本书似乎针对那些已经熟练使用javascript并正在研究更好地构建自己的j的人。


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'.

解决方案

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.

这篇关于美元符号的含义包含JavaScript脚本以外的HTML脚本标记的花括号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆