I have a small piece of code here, which is a model of some situation I am stuck with. For me as a PHP programmer this is so easy to do in PHP, but I never know how to do this in JS. Maybe someone can help.
var counterObj = {
items: [1,4,7],
total: 0,
run: function() {
$.each(this.items, function(i, item){
// Call my own method
this.add(item);
})
.promise()
.done(function(){
// doubt it will log 12
console.log(this.total);
});
},
add: function(item){
this.total = this.total + item;
}
};
counterObj.run();
.promose()is that a new method or somethingthisis referring to the number in the array.