警报功能在coffeescript中不起作用 [英] Alert function not working in coffeescript

查看:92
本文介绍了警报功能在coffeescript中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在博客中发现了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中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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