从anom访问实例variables。 function在咖啡脚本

我试图从anom函数访问@cols,即使它被定义,它仍然是未定义的(在第二行)。

 csv = require'csv'

class级库存

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

         #读取文件并推送到cols
         CSV()。
         fromPath(@文件,列:true)。
        在'数据'上,(d,索引) - >
             #推到cols
             console.log @cols

 inventory = new Inventory(__ dirname +'/sample.csv')

你需要使用胖箭头而不是苗条的箭头。

(d, index)=> 

使用=>而不是 – >,所以你可以使用@并获得对外部实例的引用

 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')