Monday, 15 February 2010

javascript - Unexpected token 'this' while creating method inside object -



javascript - Unexpected token 'this' while creating method inside object -

i creating object , go on error message unexpected token:this when add together method. here's 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 " + this.name + "<br>i " this.age + "years old." + "<br>i " + this.gender + ".<br>my career " + this.job +"."; } //object method } var colin = new person("colin james",24,"man","social media consultant"); // create new person.

i have read various articles creating methods within objects , don't see going wrong here. when remove this. syntax name, age, gender, job variables in method pspeak, error unexpected identifyer.

any suggestions whats happening?

you're missing + when setting 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 " + this.name + "<br>i " + this.age + "years old." + "<br>i " + this.gender + ".<br>my career " + this.job +"."; } //object method } var colin = new person("colin james",24,"man","social media consultant"); // create new person.

javascript object methods this

No comments:

Post a Comment