Unexpected token 'this' when creating a method inside an object

I create an object and I continue to receive an unexpected token of error message: this is when I add a method. Here is my code.

function Person(name,age,gender,job) {
    this.name = name,
    this.age = age,
    this.gender = gender,
    this.job = job,
    this.pSpeak = function() { 
        func.innerHTML = "My name is " + this.name + "<br>I am " this.age + "years old." + "<br>I am a " + this.gender + ".<br>My career is " + this.job +".";
        } //Object Method
    }

 var colin = new Person("Colin James",24,"man","Social Media Consultant"); // create a new Person.

I read various articles about creating methods inside objects, and I don’t see where I made a mistake here. When I remove the syntax this.from the name, age, gender, job variables in the pSpeak method, I get an unexpected identifier error.

Any suggestions on what's going on?

+4
source share
1 answer

You are missing out +on tuning innerHTML.

function Person(name,age,gender,job) {
    this.name = name,
    this.age = age,
    this.gender = gender,
    this.job = job,
    this.pSpeak = function() { 
        func.innerHTML = "My name is " + this.name + "<br>I am " + this.age + "years old." + "<br>I am a " + this.gender + ".<br>My career is " + this.job +".";
        } //Object Method
    }

 var colin = new Person("Colin James",24,"man","Social Media Consultant"); // create a new Person.
-2
source

All Articles