I'm learning object oriented Javascript and am trying to solve a very simple problem. Given a string, convert it to another using the rules below:
- G >> C
- C >> G
- T >> A
- A >> U
Here is my code:
var DnaTranscriber = function(){};
DnaTranscriber.prototype = {
map: {G:'C', C:'G', T:'A', A:'U'},
toRna: function(dna) {
return dna.split('').map(this.mapper).join('');
},
mapper: function(dnaChar){
if(this.map[dnaChar]) return this.map[dnaChar];
throw new Error('Invalid input');
}
};
var d = new DnaTranscriber();
d.toRna('C');
However, trying to execute this code throws an error:
TypeError: Cannot read property 'C' of undefined
Why is this.map
undefined inside mapper
?