Techwali
Techwali

Reputation: 15

property not functioning properly in my custom constructor - Javascript

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.

Upvotes: 0

Views: 36

Answers (1)

balajisoundar
balajisoundar

Reputation: 571

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;`

Upvotes: 1

Related Questions