Why does following snippet work 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
};
Even though student
comes after the function where it's being used and student
is not hoisted still why does the above function work?
But in contrast to the above example, this one:
(function(){
console.log("Name is :"+student);
})();
var student = {
name:"xyz"
};
Shows student
as undefined
means it wasn't hoisted.