1

我正在尝试从异常函数访问@cols,即使它已定义,它也未定义(在倒数第二行)。

csv = 需要'csv'

类库存

    构造函数:(@file)->
        @cols = {}      

        #读取文件并推送到cols
        .csv()。
        从路径(@文件,列:真)。
        在“数据”上,(d,索引)->
            #推送到列
            控制台.log @cols

库存 = 新库存(__dirname + '/sample.csv')
4

2 回答 2

3

您需要使用粗箭头而不是细箭头。

(d, index)=>
于 2012-05-06T16:52:12.687 回答
2

使用 => 而不是 -> 这样您就可以使用 @ 并获取对外部实例的引用

csv = require 'csv'

class Inventory

    constructor: (@file) ->
        @cols = {}      

        #Read the file and push to cols
        csv().
        fromPath(@file,columns: true).
        on 'data', (d,index) =>
            #push to cols
            console.log @cols

inventory = new Inventory(__dirname + '/sample.csv')
于 2012-05-06T16:55:18.507 回答