class - JavaScript: change a variable with a prototype function inside the constructor -


i'm new "classes" in javascript hope i've used right terms in question.

i tought this.variables in constructor used through functions defined prototype. seems right in 1 direction i'm not able update variable within function.

here 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 update this.testvar function test.changetestvar, isn't saved inside object later use. if set directly saved. can explain me why code behaves behaves , have change?

thank much

changetestvar function, , it's written in different case

var mytest = new littletest(); console.log(mytest.testvar); // -> blub  mytest.changetestvar("foo"); console.log(mytest.testvar); // -> foo 

fiddle


Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -