0

i'm new to "classes" in JavaScript so i hope i've used the right terms in my question.

I tought the this.Variables in the Constructor could be used through all the functions defined with prototype. This seems right in one direction but i'm not able to update the variable from within a function.

Here is a little jsfiddle https://jsfiddle.net/2g4vnL9b/

var littleTest = function() {
  this.testVar = "blub";
}
littleTest.prototype = {
  changeTestVar: function(val) {
    this.testVar = val;
  },
  changeTestVar2: function(val) {
    return val;
  }
}

var myTest = new littleTest();
console.log(myTest.testVar); // -> blub

myTest.changeTestVar = "foo";
console.log(myTest.testVar); // -> blub

myTest.testVar = myTest.changeTestVar2("foo");
console.log(myTest.testVar); // -> foo

i'm trying to update the this.testVar from the function test.changeTestVar, but it isn't saved inside the object for later use. Only if i set it directly it is saved. Can somebody explain me why my code behaves like it behaves and what i have to change?

thank you very much

3
  • 1
    I think this line is wrong myTest.changeTestVar = "foo"; Probably you mean myTest.changeTestVar("foo"); ? Commented Feb 8, 2016 at 15:23
  • In changeTestVar2 you are not setting this.testVar2 you are just returning what is passed in Commented Feb 8, 2016 at 15:23
  • Notice, that after getting this to work, setting this.testVar in the method creates an own property to the instance called the method. testVar in the prototype keeps its value, and when you create new instances of littleTest, testVar in those instances will be "blub". Commented Feb 8, 2016 at 15:44

4 Answers 4

1

You need to call changeTestVar as a function, not overwrite it with a new value.

Try the following:

myTest.changeTestVar("foo");
console.log(myTest.testVar);

Also, changeTestVar2 doesn't change this variables at all, it just returns the value passed in. That's normally called an "identity" function, but it doesn't have any persisting effect on your object.

Sign up to request clarification or add additional context in comments.

Comments

0

ChangeTestVar is a function, and it's written in different case

var myTest = new littleTest();
console.log(myTest.testVar); // -> blub

myTest.changeTestVar("foo");
console.log(myTest.testVar); // -> foo

FIDDLE

2 Comments

after i've read my question again i felt so stupid... it needs 10 minutes that i can mark your answer as my answer, thank you
No problem, we all make silly and strange mistakes from time to time.
0

The reason you code won't work is because your overwriting your prototype functions

myTest.changeTestVar = "foo"; //this is replacing the function for a string
console.log(myTest.testVar); // -> blub

since your calling a function the value should be passed like any other fucntion

myTest.changeTestVar("foo");
console.log(myTest.testVar); // -> foo

Comments

0

you were wrong when passing the parameter to the function, by the way you can change the variable directly

var myTest = new littleTest();
console.log(myTest.testVar);

myTest.testVar="foo";
console.log(myTest.testVar); 

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.