Not sure about instance variable scenario in Typescript -


here's class (ignore console.logs left on attempt debug)

import fs = require('fs');  export interface answer {   order: number,   text: string }  export class config {   answers:answer[];   timestamp_column: string;   name_column: string    fromjsonfile(filename: string) {     var filestring = fs.readfilesync(filename);     this.answers = json.parse(filestring.tostring());     console.log(this.answers);   }    mapanswertonum(answer:string):number {     console.log(typeof this.answers);     (var in this.answers) {       if (a.text == answer) { return a.order;}     }     (var in this.answers) {       console.log(a.text);     }     console.log(typeof this.answers);     throw new error(`invalid response string ${answer}`);   } } 

and here's invocation:

import { config } './config';  var config = new config(); config.fromjsonfile("./csvconfig.json");  ...  var respints:number[] = respstrings.map(this.config.mapanswertonum);  ... 

the question: inside body of mapanswertonum, seems this.answers undefined, though in body of fromjsonfile is defined.

am being hosed this. what's right way code intention? thanks!

since mapanswertonum defined on config's prototype, function being passed map function shared across instances of config. in case, context being lost.

you can fix changing code following:

var respints:number[] = respstrings.map((respstring) => this.config.mapanswertonum(respstring)); 

that call mapanswertonum on this.config's instance.


here's example:

class config {     mapanswertonum() {         console.log(this);     } } 

outputs:

var config = (function () {     function config() {     }     config.prototype.mapanswertonum = function () {         console.log(this);     };     return config; })(); 

so when using it:

function dummyarraymap(func) {     func(); }  var config = new config();  // logs window since function shared across instances of `config`. dummyarraymap(config.mapanswertonum); // context lost. // logs instance of config since called function on instance dummyarraymap(() => config.mapanswertonum()); 

alternative this, use arrow function on mapanswertonum. isn't solution because function no longer on prototype. i'll demonstrate example above:

class config {     mapanswertonum = () => {         console.log(this);     } } 

outputs:

var config = (function () {     function config() {         var _this = this; // notice saved here         this.mapanswertonum = function () {             console.log(_this); // used here         };     }     return config; })(); 

so when using it:

var config = new config();  dummyarraymap(config.mapanswertonum);         // logs config dummyarraymap(() => config.mapanswertonum()); // logs config 

Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

Rendering JButton to get the JCheckBox behavior in a JTable by using images does not update my table -