简体   繁体   English

全局变量未更新

[英]Global variable is not updated

I'm new to coffeescript . 我是coffeescript And I fell into the pit of the JavaScript's variable scope. 我陷入了JavaScript可变范围的困境。

I'm trying to develop simple console script with node.js , no classes (yet), minimum functional programming sugar (yet). 我正在尝试使用node.js开发简单的控制台脚本,尚无类(尚未),最低功能的编程糖(尚)。 Here it goes: 它去了:

fs      = require 'fs'
code    = "test"

fs.readFile 'COD99000430.TXT', (err, contents) ->
    code = contents.toString()

console.log code

it compiles to following JavaScript: 编译为以下JavaScript:

// Generated by CoffeeScript 1.3.3
(function() {
  var code, fs;

  fs = require('fs');

  code = "test";

  fs.readFile('COD99000430.TXT', function(err, contents) {
    return code = contents.toString();
  });

  console.log(code);

}).call(this);

The issue is that after running that code console shows only word test , which is obviously initial value of the code variable. 问题在于,运行该代码控制台后,仅显示单词test ,这显然是code变量的初始值。 But I expect to see contents of the COD99000430.TXT file. 但是我希望看到COD99000430.TXT文件的内容。 And actually I can see it, if i put console.log code inside of the scope of the anonymous callback function in the fs.readFile call. 如果我将console.log code放在fs.readFile调用中的匿名回调函数的范围内,则实际上可以看到它。

I've look at coffieescript documentation and there exactly the same case was described. 我查看了coffieescript文档 ,并描述了完全相同的情况。 But for some reason it does not work for me. 但是由于某种原因,它对我不起作用。

I'm using node.js version 0.8.2 on Windows 7, coffeescript version 1.3.3. 我在Windows 7, coffeescript版本1.3.3上使用node.js版本0.8.2。

Is it possible to have "global script" variable in my case? 在我的情况下,是否可以使用“全局脚本”变量? How I can achieve this via coffeescript? 我如何通过coffeescript实现这一目标?

The comments on your question are correct, I just thought I'd provide some working code. 关于您的问题的评论是正确的,我只是想提供一些工作代码。

Example 1: Simply log the file contents in the fs.readFile callback. 示例1:只需将文件内容记录在fs.readFile回调中。

fs = require 'fs'

fs.readFile 'COD99000430.TXT', (err, contents) ->
  console.log contents.toString()

Example 2: Factor the file processing code into a function and provide a callback to print the contents. 示例2:将文件处理代码分解为一个函数,并提供回调以打印内容。

fs = require 'fs'

processFile = (filename, callback) ->
  fs.readFile filename, (err, contents) ->
    callback contents.toString()

processFile 'COD99000430.TXT', (data) -> console.log data

I modified your code a bit so I could get it running in jsfiddle (had to ditch node since it isn't installed on this machine). 我对代码进行了一些修改,以便可以在jsfiddle中运行它(由于未在该计算机上安装该节点,因此必须放弃该节点)。 I think what you need to is change the readFile line to be a function, then call the function later. 我认为您需要将readFile行更改为一个函数,然后再调用该函数。

code = "test"

read = (err, contents) ->
    code = "XZY".toString()

console.log code
alert(code)

code = read()
alert(code)  

Global variables are considered 'to be avoided'. 全局变量被视为“应避免”。 The coffeescript page that you linked to does have an alternative though: 您链接到的coffeescript页面确实有一个替代方法:

If you'd like to create top-level variables for other scripts to use, attach them as properties on window, or on the exports object in CommonJS. 如果要创建供其他脚本使用的顶级变量,请将它们作为属性附加到window或CommonJS中的exports对象上。 The existential operator (covered below), gives you a reliable way to figure out where to add them; 存在运算符(见下文)为您提供了一种可靠的方法来确定将它们添加到何处。 if you're targeting both CommonJS and the browser: exports ? 如果您同时定位CommonJS和浏览器:exports? this 这个

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM