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.

1
  • 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. Commented Mar 10, 2015 at 8:04

1 Answer 1

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;`
Sign up to request clarification or add additional context in comments.

2 Comments

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)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.