I have the following code however am getting the error Uncaught TypeError: Object #<addThis> has no method 'returnValue' (anonymous function)
function addThis() {
this.value1 = 1;
this.value2 = 2;
var returnValue = function () {
return (this.value1 + this.value2);
}
}
//Instantiate object and write response
var simpleObject = new addThis();
document.write(simpleObject.returnValue());
Because
returnValueis just a local variable in theaddThisfunction, it doesn’t end up in the object that is created.Assign the function to a property of the object:
Or use the prototype for the object: