为什么JavaScript函数调用中没有(){paranethes}
这是index.js文件的代码快照,默认情况下在新的phonegap项目中创建为什么JavaScript函数调用中没有(){paranethes},javascript,cordova,phonegap-build,Javascript,Cordova,Phonegap Build,这是index.js文件的代码快照,默认情况下在新的phonegap项目中创建 var app = { // Application Constructor initialize: function() { this.bindEvents(); }, // Bind Event Listeners // // Bind any events that are required on startup. Common events ar
var app = {
// Application Constructor
initialize: function() {
this.bindEvents();
},
// 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() {
app.receivedEvent('deviceready');
},
// 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);
}
};
在第11行
document.addEventListener('deviceready', this.onDeviceReady, false);
我假设
this.ondevicerady
是一个函数调用,那么为什么这里没有像this.ondevicerady()
这样的。在函数上使用()
时,将立即调用它
当使用函数引用时,函数被传递给另一个函数,当某个事件发生时,函数被调用
这和
function somefun(callback) {
// When something ASYNCHRONOUS process completes, call the callback function
callback();
}
var myFun = function() {
console.log('in myFun');
};
function somefun(myFun);
this.ondevicerady
是这里的函数引用。在函数上使用()
时,将立即调用它
当使用函数引用时,函数被传递给另一个函数,当某个事件发生时,函数被调用
这和
function somefun(callback) {
// When something ASYNCHRONOUS process completes, call the callback function
callback();
}
var myFun = function() {
console.log('in myFun');
};
function somefun(myFun);
如果在将函数作为引用传递时将()与this.ondevicerady
一起使用,则会立即调用ondevicerady()方法。如果在将函数作为引用传递时将()与this.ondevicerady
一起使用,则会立即调用ondevicerady()方法。这不是函数调用,这是一个函数。不是函数调用,而是一个函数。@kashyapkotak对。@kashyapkotak对。