iT邦幫忙

1

[筆記系列]Jquery / Javascript 物件導向OO宣告寫法

window["這支js的名稱"] = window["這支js的名稱"] || (function () {
    try {
        var ns = function (info) {
            var ff = this;

            for (var key in info) {
                this[key] = info[key];
            }

            if (ff.mainObj == null) { throw "must be have mainObj."; }
            ff.mainObj.data(ns.moduleName, ff);
        };

        ns.moduleName = "這支js的名稱";
        ns.prototype.getModuleName = 
        function () { return ns.moduleName; }
        ns.jQueryPluginName = "這支js的名稱";
        ns.windowPluginName = "這支js的名稱";

        //Method這裡是一些變數的宣告區域 有點像是全域變數 以下變數皆為舉例使用
        ns.prototype.getModuleName = 
        function () { return ns.moduleName; }
        ns.alert = 
        function (msg, func) {
            if (func == null) 
            { alert("Error(" + ns.moduleName + "): " + msg); }
            else 
            { alert("Error(" + ns.moduleName + "." + func + "): " + msg); }
        };
        ns.prototype.PreFormCategory = "";
        ns.prototype.FlowTestUnitLst = [];
        ns.prototype.disableValArr = ["依實際數量", "依剩餘數量"];
        ns.prototype.Asy_DRAM_Last_Step = "LS_MARK0";
        ns.prototype.AllowedOwnerCode = ["C", "X", "T"];
        ns.prototype.AllowShift = "Y";
        ns.prototype.is_DE_User = false;
        
        
        
        /*---End Template - Basic Code------------------------*/



        //EX Function

        //ns.開頭方法
        ns.testFunction_01 = function (e) {
            //do something
        };

        //window[""]開頭方法
        window["testFunction_02"] = function (e) {
            //do something
        };

        return ns;
    } catch (ex) {
        WbVir.WvErrorHandler.showAlert("Initial 這支js的名稱 fail: " + ex);
    }
})();

在Html的tag 寫onclick事件寫法 EX:

<input type="button" id="btn_test_01" onclick="這支js的名稱.testFunction_01()">

//重點
ns.testFunction_01 用法 = window["testFunction_02"]用法

更新 於其他js使用

$(document).ready(function () {
    $("#btn_click").click(function () {
        alert(這支js的名稱.testFunction_01());
    });
});

如有謬誤請各路大神指導
純屬筆記系列


圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 則留言

0
神Q超人
iT邦研究生 5 級 ‧ 2018-02-14 14:44:34

這是宣告一個全域物件的感覺嗎/images/emoticon/emoticon13.gif

我要留言

立即登入留言