jeudi 7 janvier 2016

Changing object's property value with method

I'm kind of new to both JavaScript and jQuery but got some programming experience on other languages. This is what I wrote:

var pageFunctionality={
value: 0,
onUpdate: function(){
    $("#numberOfTimes").html(pageFunctionality.value);
    console.log(pageFunctionality.value + "!");
},
onBtnClick: function(){
    this.value++;
    console.log(this.value);
    pageFunctionality.onUpdate();
}
};
$(document).ready(function(){
    $("#btn").click(pageFunctionality.onBtnClick);
});

The problem is this: when I log the "value" inside the "onBtnClick", it clearly increases, but then, when I log it inside the onUpdate method, it keep being 0.

This is the output it produces:

1
0!
2
0!
3
0!
4
0!

I hope you can help me.




Aucun commentaire:

Enregistrer un commentaire