在对象内创建方法时意外的令牌'this'



我正在创建一个对象,当我添加一个方法时,我继续收到错误消息意外令牌:this。这是我的密码。

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.

我读过很多关于在对象中创建方法的文章,我没有发现哪里出了问题。当我从方法pSpeak中的名称、年龄、性别、工作变量中删除this.语法时,我会得到错误意外标识符

对发生的事情有什么建议吗?

设置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.

最新更新