I am defining an object literal in JavaScript, but when I try to create a property that is a called function, I get a Uncaught ReferenceError: xxx is not defined error.
Here's an example:
var person = {
name : 'Saucy Jack',
convenienceHook : this.getName('some predefined string'),
getName : function(string) {
console.log(this.name + string);
}
};
This errors saying Uncaught ReferenceError: convenienceHook is not defined.
Nor does it work if I assign the property outside the object definition:
var person = {
name : 'Saucy Jack',
getName : function(string) {
console.log(this.name + string);
}
};
person.convenienceHook = person.getName('some predefined string');
person.convenienceHook(); //returns error
What am I missing?
getName()isn't defined by the time you call it as the object instantiation isn't complete. In the second instance,getName()doesn't return anything (or returnsundefined) which isn't a function.TypeError: this.getName is not a function)