Meaning of Dollar Sign & Curly Braces Containing Javascript Block Outside of HTML Script Tag

有些话、适合烂在心里 提交于 2019-12-21 04:57:32

问题


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


回答1:


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.



来源:https://stackoverflow.com/questions/8674711/meaning-of-dollar-sign-curly-braces-containing-javascript-block-outside-of-htm

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!