在对象内创建方法时出现意外的标记'this'

时间:2014-10-19 01:20:04

标签: javascript object methods 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.语法时,我收到错误意外的标识符

有关最新情况的任何建议吗?

1 个答案:

答案 0 :(得分:-1)

设置+时,您错过了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.