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.

 

jemsicon

 

Consider the following:

 

myfunc=function(){
 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(){
 if(browser=="IE"){
  myfunc=function() {alert("Browser 1")};
 } else {
  myfunc=function() {alert("Browser 2")};
 };
 myfunc();
};

 

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";
alert(myfunc);
myfunc();
alert(myfunc);

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.

 

blog comments powered by Disqus

 

Banner


Getting Started With jQuery - Advanced Filters

When you first encounter filters they seem easy enough - just extract the results you want from the results you have. The trouble is that filters are fun and jQuery pushes the idea beyond the obvious. [ ... ]



Getting Started With jQuery - Filters

Mastering the core of jQuery is first a matter of understanding selectors and then DOM manipulation. Filters are often confused with selectors but they are quite different and serve an important purpo [ ... ]


Other Articles

 

<ASIN:0596517742>

<ASIN:0321572602>

<ASIN:0596806752>

<ASIN:1590597273>

<ASIN:059680279X>

<ASIN:0596805527>



Last Updated ( Thursday, 26 December 2013 )
 
 

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