Can JavaScript Rewrite Itself? A Self‑Modifying Code Demo for Thanksgiving

This article explains the concept of self‑modifying code, shares a JavaScript example that changes its behavior on Thanksgiving, and discusses when such dynamic techniques are appropriate in modern development.

21CTO
21CTO
21CTO
Can JavaScript Rewrite Itself? A Self‑Modifying Code Demo for Thanksgiving

Twenty‑five years ago the author started software development and met Dave, a former insurance‑company programmer who maintained the backend for a personal life‑insurance product.

What Is Self‑Modifying Code?

Self‑modifying code is code that alters its own instructions at runtime, often to reduce instruction length, improve performance, or simplify maintenance by eliminating redundancy. Historically it was used to maximize memory usage on machines with very limited RAM.

JavaScript Example

Inspired by this concept, the author wrote a JavaScript function that rewrites itself on Thanksgiving (November 23, 2017). The function checks the current date and, if it matches Thanksgiving, redefines itself to log “吃火鸡” ("eat turkey"); otherwise it logs “去工作” ("go to work").

selfModifyingCode = function(){
    var turkeyDay = new Date("November 23, 2017 01:00:00");
    var date = new Date();
    if (date.getDate() === turkeyDay.getDate()){
        selfModifyingCode = function(){
            console.log("吃火鸡");
        };
    } else {
        selfModifyingCode = function(){
            console.log("去工作");
        };
    }
    selfModifyingCode();
};
selfModifyingCode();

When the function runs, it first creates two date variables, compares them, and then overwrites itself with a simpler version that directly logs the appropriate message. Subsequent calls therefore execute the rewritten version without re‑evaluating the dates.

Conclusion

Self‑modifying code can be an effective way to handle runtime logic decisions, but it should be used only when truly beneficial, as it can make maintenance difficult. In the era of limited memory, such techniques were sometimes necessary, but modern systems usually have better alternatives.

While the presented example is simple, many more robust approaches exist for achieving the same outcome without rewriting functions at runtime.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

frontendJavaScriptProgramming Conceptsdynamic functionsself-modifying code
21CTO
Written by

21CTO

21CTO (21CTO.com) offers developers community, training, and services, making it your go‑to learning and service platform.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.