How is it possible that the following code snippet works in Angular JS?
var app = angular.module('store',[]);
(function(){
app.controller('StoreController',function(){
this.blabla = student;
});
})();
var student =
{
name:"Abc Def",
rollNumber:12,
isBrilliant: true,
isMale:false,
isFemale: false,
istest:true
};
Despite the fact that student
is defined after the function where it's being used and student
isn't hoisted, why does the above function still work?
On the other hand, consider this example:
(function(){
console.log("Name is :"+student);
})();
var student = {
name:"xyz"
};
This code snippet displays student
as undefined
, indicating that it wasn't hoisted.