[英]How to pass a function that's a property of another object into a function and call it in javascript?
[英]How to pass a specific property into a javascript object in a function
我繼承了一個舊的代碼庫,我公司中沒有人使用過。 有一個jquery插件正在使用,且文檔最少。 這是我需要的部分:
/**
* @param {String} message This is the message string to be shown in the popup
* @param {Object} settings This is an object containing all other settings for the errorPopup
* @param {boolean} settings.close Optional callback for the Okay button
* @returns a reference to the popup object created for manual manipulation
*/
Popup.errorPopup = function(message , settings ){
settings = settings || {};
var defaults = {
allowDuplicate: false,
centerText: true,
closeSelector: ".ConfirmDialogClose"
}
settings = $.extend( defaults , settings );
return Popup.popupFactory( message,
settings,
".ConfirmDialogBox",
".PopupContent"
);
}
我們當前對該函數的調用僅使用默認設置。 他們都沒有傳遞任何東西。例如:
Popup.errorPopup('Sorry, your account couldn\'t be found.');
對於此的一種用法,我需要在彈出窗口關閉時傳遞一個回調函數。 根據評論,有一個settings.close
參數,但是我不知道如何通過函數調用傳遞它。
我嘗試了這個:
Popup.errorPopup('Sorry, your account couldn\'t be found.', {close: 'streamlinePassword'});
其中streamlinePassword
是回調函數的名稱。
但是出現了一個JavaScript錯誤:對象#的屬性“關閉”不是一個函數。
如何將這個新的對象參數傳遞給函數調用?
不傳遞字符串,傳遞函數。
樣品:
function streamlinePassword() {
// ...
}
Popup.errorPopup('...', {close: streamlinePassword});
// also possible
Popup.errorPopup('...', {
close: function () {
}
});
// also possible II
Popup.errorPopup('...', {
close: function test() {
}
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.