Javascript Jems - Self Modifying Code
Javascript Jems - Self Modifying Code
Written by Ian Elliot   
Tuesday, 24 December 2013
Article Index
Javascript Jems - Self Modifying Code
Safer self-modification

Simpler self modification

However it is worth mentioning that some of the most useful forms of self modification are much simpler and much safer than the general scheme outlined above.




Consider the following:


 if(browser=="IE") {
  alert("Browser 1");
 } else  {
  alert("Browser 2");


In this case myfunc has to deal with the possibility that it might be run on one of two types of browser and what it has to do is browser specific. It does this using a simple if statement but each time the function is called the selection has to be made even though once loaded the browser type cannot change.

That is, each time myfunc is called it has to check the state of the browser variable even though it can’t have changed since the last time it was used.

We can make this all much more efficient by simply redefining the function depending on which version of the browser it finds itself in:


  myfunc=function() {alert("Browser 1")};
 } else {
  myfunc=function() {alert("Browser 2")};


Notice that the system is sophisticated enough to buffer the function definition so that changing it mid-way through its execution has no effect. However, the new call to the function at the end of the modification does run the new function.

If you find this confusing simply compare the function’s definition before and after it has been run:

var browser="IE";

Before the first call the function is as listed above complete with the if statement. After the first call it simply reads:

function() {alert("Browser 1")};

and no test is made to see which browser it is being run in until it is started afresh in possibly a different browser.

This is a very attractive idea. Creating self modifying functions in this way is a safe thing to do and it is used in a number of JavaScript libraries.

However self-modifying code is dangerous and often over complex– use it with great care and not very often.


Related Articles

Javascript Jems - First class functions

Chaining - Fluent Interfaces In JavaScript

JavaScript Objects With Value - valueOf and toString


To be informed about new articles on I Programmer, install the I Programmer Toolbar, subscribe to the RSS feed, follow us on, Twitter, FacebookGoogle+ or Linkedin,  or sign up for our weekly newsletter.






or email your comment to:



JavaScript Async - Advanced Worker Threads

Worker threads are increasingly important in modern websites as well as web apps because they allow you to do long running tasks without blocking the UI thread. However they don't, as many believe, re [ ... ]

Just JavaScript - The Object Expression

As in most programming languages, the expression is an important part of JavaScript, but it isn't quite the same. This is where the idea that JavaScript has some weird type conversions arises. But in  [ ... ]

Other Articles








Last Updated ( Thursday, 26 December 2013 )

RSS feed of all content
I Programmer - full contents
Copyright © 2018 All Rights Reserved.
Joomla! is Free Software released under the GNU/GPL License.