Alert Function Not Working In Coffeescript
Solution 1:
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).
When JavaScript is embedded in a browser, the global object is window
. But in Node.js, the global object is simply global
.
Some methods are available in both environments, like core JavaScript methods...
String.prototype
methodsArray.prototype
methodsObject.prototype
methods- etc.
... and specific window
methods like setInterval
or setTimeout
.
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
.
JavaScript
// alert.jsvar alert = require('alert-node');
alert('Hello');
Command:node alert.js
CoffeeScript
# alert.coffee
alert = require'alert-node'
alert 'Hello'
Command:coffee alert.coffee
Solution 2:
window.alert
is a method defined by the DOM (in browsers), not by Javascript. If the environment you're running this in doesn't have a global alert
method defined, then you can't call it.
Post a Comment for "Alert Function Not Working In Coffeescript"