且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Js Pattern - Self Define Function

更新时间:2022-08-18 08:01:14

This pattern is useful when your function has some initial preparatory work to do and
it needs to do it only once.In such cases, the selfdefining function can update its own implementation.

eg:

var selfFunc = function () {
        console.log("First Initialization!");
        selfFunc = function () {
            console.log("-- Function Logic --");
        };
    };

    window.onload = function () {
        selfFunc();
        selfFunc();
        selfFunc();
        console.log("<br />");
    };

 

Result:

First Initialization!
-- Function Logic --
-- Function Logic --