What is the difference between the following two code segments:
function HelloService(){
var service = this;
service.itemList = []
service.hello = function(){
return "Hello World!!";
};
service.addItem = function(){
service.itemList.push(1);
}
}
function HelloService(){
var service = this;
var itemList = [];
var hello = function(){
return "Hello World!!";
};
service.addItem = function(){
itemList.push(1);
}
}
Because as far as I understand the this inside the hello function and outside the hello function points to the same instance.
Could someone explain the above problem w.r.t to JAVA?
EDIT: I have added an addItem function. Here I don't understand the difference between service.itemList and var itemList inside the addItem function. Could you explain the difference inside that function?
thisinside the function, depends entirely on how you call the functionthis.hello) you can call the hello function from the outside (console.log(new HelloService().hello());). In the second one hello is just a local variable, not accessible from the outside.thisis at all, as the variable has nothing to do withthis.thisis the same inside and outside the function, the function creates it's own scope, and the variable is declared in that scope, and as variables are function scoped, they are only accessible within that scope (or a "lower" scope).