kashyap kotak kashyap kotak - 1 year ago 49
Javascript Question

Why there is not () in javascript function calling

this is a code snap of index.js file which is created by default in a new phonegap project.

var app = {
// Application Constructor
initialize: function() {
// Bind Event Listeners
// Bind any events that are required on startup. Common events are:
// 'load', 'deviceready', 'offline', and 'online'.
bindEvents: function() {
document.addEventListener('deviceready', this.onDeviceReady, false);
// deviceready Event Handler
// The scope of 'this' is the event. In order to call the 'receivedEvent'
// function, we must explicitly call 'app.receivedEvent(...);'
onDeviceReady: function() {
// Update DOM on a Received Event
receivedEvent: function(id) {
var parentElement = document.getElementById(id);
var listeningElement = parentElement.querySelector('.listening');
var receivedElement = parentElement.querySelector('.received');

listeningElement.setAttribute('style', 'display:none;');
receivedElement.setAttribute('style', 'display:block;');

console.log('Received Event: ' + id);


on line 11,

document.addEventListener('deviceready', this.onDeviceReady, false);

I assume that
is a funtion call so why there is no

Answer Source

this.onDeviceReady is function reference here. When used () on function, it'll be called immediately.

When used function reference the function is passed to the other function and when some event occurs the function is called.

This is same as

function somefun(callback) {

    // When something ASYNCHRONOUS process completes, call the callback function

var myFun = function() {
    console.log('in myFun');

function somefun(myFun);