Javascript function call .
Posted: Tue Nov 04, 2014 12:27 pm
The answer might be obvious , but i still don't get it , check out the below snippet :
it has two functions show_items() one as a global function and the other as a function inside temp , i have commented what i get in the console .
now when i make one small change I.E. i make the function inside temp a self calling function :
check out the code below , i have commented what i get in the console .
Why ?? when i make the function inside temp a self calling function , what exactly changes , or rather what exactly is it calling ??
it has two functions show_items() one as a global function and the other as a function inside temp , i have commented what i get in the console .
Code: Select all
<script type="text/javascript">
var movies = 'Love them !!',
radio = 'antique';
var temp = {
movies : 'Love them !! inside temp',
radio : 'antique.. inside temp',
show_items : function show_items(){
console.log(this.movies + " " + this.radio)
}
}
function show_items(){
console.log(this.movies + " " + this.radio)
}
show_items();
temp.show_items();
i get the below in the console :
Love them !! antique
Love them !! inside temp antique.. inside temp
</script>
Code: Select all
show_items : function show_items(){
console.log(this.movies + " " + this.radio)
}()
Code: Select all
var movies = 'Love them !!',
radio = 'antique';
var temp = {
movies : 'Love them !! inside temp',
radio : 'antique.. inside temp',
show_items : function show_items(){
console.log(this.movies + " " + this.radio)
}()
}
function show_items(){
console.log(this.movies + " " + this.radio)
}
show_items();
i get the following on the console :
Love them !! antique
Love them !! antique