1

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());

3 Answers 3

1

Because returnValue is just a local variable in the addThis function, it doesn't end up in the object that is created.

Assign the function to a property of the object:

function addThis() {
  this.value1 = 1;
  this.value2 = 2;

  this.returnValue = function() {
    return this.value1 + this.value2;
  };
}

Or use the prototype for the object:

function addThis() {
  this.value1 = 1;
  this.value2 = 2;
}

addThis.prototype.returnValue = function() {
  return this.value1 + this.value2;
};
Sign up to request clarification or add additional context in comments.

2 Comments

Do I have to specify the this keyword when assigning returnValue as a property of the addThis object? Can I not just have returnValue = ....? If no, why is that?
@PeanutsMonkey: Yes, you always have to specify the object when accessing the properties, either using this.propertyName or someVariable.propertyName. The scope is never inside the object like in an object oriented language. If you use var returnValue = ... it is a local variable, and if you use just returnValue = ... then it becomes a global variable (unless you have var returnValue; anywhere else in the function).
1

when you use this. it is public in scope. when you use var, it is private. since you used var returnValue, it is private, and thus not exposed for use.

In fact, i'm guessing you wanted to hide the values and expose the getter, so reverse what you did..

function addThis() {
    var value1 = 1;
    var value2 = 2;

    this.returnValue = function () {
        return (this.value1 + this.value2);
    }
}

6 Comments

Thanks. What do you mean by "In fact, i'm guessing you wanted to hide the values and expose the getter, so reverse what you did..". Sorry am new to the world of Javascript so want to make sure that I understand what you mean.
Does public scope also mean global scope? I thought this was limited to the object in question as opposed to being in the public scope?.
@PeanutsMonkey no, global means your variable is actually within the window namespace. so instead of writing var x = 2, your global would be simply x = 2, which is the same as saying window.x = 2;
Okay I get that but how is this in the global namespace of window?
@PeanutsMonkey: Everything is public in Javascript, there is no access control like in an object oriented language. The "private" variables in the example are only protected by the fact that there is no way to describe how to access them from outside the function.
|
1

var will declare a variable local to the function. I think you meant to assign it to this.returnValue:

function addThis() {
    this.value1 = 1;
    this.value2 = 2;

    this.returnValue = function () {
        return (this.value1 + this.value2);
    };
}

// Instantiate object and write response
var simpleObject = new addThis();
document.write(simpleObject.returnValue());

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.