var someObject = {
someArray : new Array(),
someInt : 0,
Total: function(){
this.someInt = 0;//we used "this" keyword here, why?Cant we just say "someInt = 0"?
for(var i=0;i<this.someArray.length;i++){//and here..
var c = this.someArray[i];//again we use "this"
this.someInt += c.value;//also here
}
so why did we use “this” keyword? cant we just type the name of the variable?
The
thiskeyword refers to the object on whose behalf the call is made later on, i.e. if you call the function like this:then
thiswill refer tosomeObjectinside the function. Thanks tothiskeyword the function can modifysomeIntand read fromsomeArraywhich are members ofsomeObject. If you droppedthisfrom the function body, all those references would be to global variables or variables local to the function body.