0

I'm trying to create a custom constructor in Javascript but I can't seem to get why the Console won't log the 'Letters' property of "Investigating" which was created by the constructor "Verb":

function Verb (tense, transitivity) {
    this.tense = tense;
    this.transitivity = transitivity;
    **this.letter1 = this.charAt(0);
    this.letter2 = this.charAt(2);
    this.letter3 = this.charAt(4);
    this.Letters = this.letter1 + " " + this.letter2 + " " + this.letter3;**

}

var Investigating = new Verb ("present", "transitive");


console.log(Investigating.tense);  // present
**console.log(Investigating.Letters); //console doesn't log anything**

What am I doing wrong here? Would appreciate any help you guys, thanks.

Techwali
  • 15
  • 7
  • Basically, I want the console to log I, V, S when I run console.log(Investigating.Letters) and the 0, 2, 4 characters for every new Verb that I create using the constructor. – Techwali Mar 10 '15 at 08:04

1 Answers1

1

Inside a constructor function this refers to the obj being created .so this.charAt(0) is incorrect.since Object object does't have charAt method up in its prototype chain.(strings and array has this method).i think you are tryin to do.

this.letter1 = this.transitivity.charAt(0);
this.letter2 = this.transitivity.charAt(2);
this.letter3 = this.transitivity.charAt(4);
this.Letters = this.letter1 + " " + this.letter2 + " " + this.letter3;`
balajisoundar
  • 581
  • 2
  • 11
  • Ah, so the Object doesn't have the charAt method... What I actually want to do is to grab the characters at 0, 2, 4 of new objects that are made using this constructor. So I want the console to log I, V, S when I run **console.log(Investigating.Letters)** – Techwali Mar 10 '15 at 07:46
  • similar question:http://stackoverflow.com/questions/789675/how-to-get-class-objects-name-as-a-string-in-javascript – balajisoundar Mar 10 '15 at 08:14