score:3

Accepted answer

A common way to do this in javascript is with a closure variable in the parent scope:

function MyClass(){
    var self = this;
    this.objects = [];
    this.add_objects = function(filename){
        d3.csv(filename, function(data){
             //Callback fired for each parsed line in csv file

             // you can access self here
             self.objects.push(xxx);
        });
    }
}

or that variable could be more specific:

function MyClass(){
    this.objects = [];
    var objectArray = this.objects;
    this.add_objects = function(filename){
        d3.csv(filename, function(data){
             //Callback fired for each parsed line in csv file

             // you can access objectArray here
             objectArray.push(xxx);
        });
    }
}

or, you can use .bind() (if you don't need to support older IE like IE8) to create a wrapper that will force the setting of this for your callback:

function MyClass(){
    this.objects = [];
    this.add_objects = function(filename){
        d3.csv(filename, function(data){
             //Callback fired for each parsed line in csv file
             this.objects.push(xxx);
        }.bind(this));
    }
}

score:2

Another approach (but more complicated) is to use bind:

function MyClass(){
    this.objects = [];
    function handler(data) {        
        // here this is your binded this.
    }
    this.add_objects = function(filename){
        d3.csv(filename, handler.bind(this));
    }
}

Related Query