Js Pattern - Self Define Function

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 --

 

 

posted @ 2013-09-22 13:10  Master HaKu  阅读(480)  评论(0编辑  收藏  举报