Experimenting with adding watchers dynamically in Vue.js. Disconnect between desired/actual results is shown in the commented portion below. Occasional/amateur coder here; I suspect my problem is less with Vue and more with understanding of JavaScript fundamentals. Thanks in advance!
new Vue({
el: '#app',
data: {
a: 1,
b: 2,
c: 3
},
methods: {
setUpWatchers(array) {
for (var i in array) {
var key = array[i];
this.$watch(key, function(newValue) {
console.log(key + ': ' + newValue);
//desired output is:
// a: 4
// b: 5
// c: 6
//actual output is:
// c: 4
// c: 5
// c: 6
});
}
}
},
created() {
this.setUpWatchers(['a', 'b', 'c']);
this.a = 4;
this.b = 5;
this.c = 6;
}
});