簡體   English   中英

如何從此函數返回值

[英]How do I return value from this function

我正在嘗試編寫一個插件,並想返回一些值:

(function ($) {
    $.SmartMessageBox = function (settings, callback) {
        var SmartMSG = 'hello', Content;

        //some code

        return SmartMSG;

    }
})(jQuery);

但是當我這樣打來電話時,打招呼永遠不會返回給呼叫者

console.log($.SmartMessageBox(null,null))

我想念什么?

這樣調用console.log($.SmartMessageBox(null,null))由於它是用$注冊的,因此您應該像上面那樣使用

您可能在執行中錯過了$ :$ .Smart MessageBox(null,null);

您的代碼在chrome控制台中執行:

(function ($) {
    $.SmartMessageBox = function (settings, callback) {
        var SmartMSG = 'hello', Content;

        //some code

        return SmartMSG;

    }
})(jQuery);
undefined

$.SmartMessageBox(null, null)
"hello"

只是在小提琴中測試了它的工作原理:

(function ($) {
    SmartMessageBox = function (settings, callback) {
        var SmartMSG = 'hello', Content;

        //some code

        return SmartMSG;

    }
})(jQuery);


console.log(SmartMessageBox(null,null));

現場測試

> `

$.fn.SmartMessageBox = function(settings, callback) {
     return "my settings";
};

Alert($.SmartMessageBox());`

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM