Can I use Coffeescript in client side too?

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-23 09:16:18

问题


Is there any way to use CoffeeScript in client side?


回答1:


There are two ways:

  1. Compile the CoffeeScript to JavaScript and deploy it as you would any JavaScript file, or
  2. Use coffee-script.js, which allows you to put <script type="text/coffeescript> tags in your page.

The latter isn't recommended for production use, but it's nice for development. See the related question: Is there a way to send CoffeeScript to the client's browser and have it compiled to JavaScript *there*?




回答2:


See also Webmake plugin for CoffeeScript -> https://github.com/medikoo/webmake-coffee

It allows you to organize coffee modules in Node.js style and bundle it for browser. It provides source maps support, so you can debug CoffeeScript files as they are, directly in a browser.




回答3:


To not compile everytime you can use -w param and coffee will compile the file everytime file change

coffee -wco src/ public/js



回答4:


Yes, it can be done by adding a CoffeeScript src tag to the head section of your html page.

Download the CoffeeScript source from this path: http://coffeescript.org/extras/coffee-script.js

Copy and paste the below code and try to run in a browser:

<html>
<head>
<script type="text/javascript">
function printHelloJava(){
alert("Hello Javascript");
}
</script>
<script src="coffee-script.js"></script>
<script type="text/coffeescript">
@printHello = ->
  alert "Hello Coffee Script"
</script>
</head>
<body>
<h1>Coffee Script on client side</h1>
<input type="button" onclick="printHelloJava();" value="Hello Java">
<br>
<input type="button" onclick="printHello()" value="Hello Coffee">
</body>
</html>



回答5:


You can also use CDN coffeescript for better and faster performance.

<script src="http://cdnjs.cloudflare.com/ajax/libs/coffee-script/1.7.1/coffee-script.min.js"></script>

or

<script src="https://cdn.rawgit.com/jashkenas/coffeescript/1.11.1/extras/coffee-script.js"></script>

Then use type="text/coffeescript" for compile Coffee Script.

<script type="text/coffeescript">
    // add code here
</script>


来源:https://stackoverflow.com/questions/6798884/can-i-use-coffeescript-in-client-side-too

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