我在博客中发现了CoffeeScript,并决定尝试一下,我的第一个项目/代码是
I found about CoffeeScript in a blog and decided to give it a try ,my first project/code with it was this
alert "Hello CoffeeScript!"它不起作用并给出此回复
It doesn't work and gives this reply
ReferenceError: alert is not defined有什么东西吗?我做错了吗?
is there anything i am doing wrong?
推荐答案JavaScript是一种与环境概念紧密相关的语言。 浏览器和 Node.js 是两种运行JS代码的环境(CoffeeScript编译为JavaScript)。
JavaScript is a language which is strongly tied to the concept of environments. A browser and Node.js are two possible environments to run JS code (CoffeeScript compiles to JavaScript).
当JavaScript嵌入浏览器中时,全局对象为 window 。但是在Node.js中,全局对象只是 global 。
When JavaScript is embedded in a browser, the global object is window. But in Node.js, the global object is simply global.
在两种环境中都可以使用某些方法,例如核心JavaScript方法...
Some methods are available in both environments, like core JavaScript methods...
- String.prototype 方法
- Array.prototype 方法
- Object.prototype 方法
- 等。
- String.prototype methods
- Array.prototype methods
- Object.prototype methods
- etc.
...以及特定的窗口方法,例如 setInterval 或 setTimeout 。
... and specific window methods like setInterval or setTimeout.
但是, window.alert 显然在CLI中不可用。如果要在Node中使用此功能,则必须使用 alert-node ---> npm我警报节点。
However, window.alert is obviously not available in CLI. If you want this functionality in Node, you will have to use something like alert-node ---> npm i alert-node.
// alert.js var alert = require('alert-node'); alert('Hello');命令: node alert.js
# alert.coffee alert = require 'alert-node' alert 'Hello'命令: 咖啡alert.coffee
更多推荐
警报功能在coffeescript中不起作用
发布评论