Web Hosting Philippines, Offshore Programming, Offshore SEO Philippines, Cheap Webhosting Manila Philippines


The R5 (Rant, Rave, Rant, Rant, Rave) Blog

Tuesday, March 11, 2008

Javascript anonymous functions - Indispensable Example #1

In this javascript tutorial on anonymous functions, we are shown how anonymous functions (or lambdas, in computer science parlance) work and what the javascript syntax for them looks like. Some people might wonder of what practical value they might be. Well, I've just come across a situation where the utility of having lambdas just shines through and is made unequivocally clear. It is in fact a very general situation which you are likely to encounter often so pay close attention. :-D

In Mozilla, if you want to attach an event to a particular DOM node, you can simply do the below:

document.getElementById("iframe1").
setAttribute("onload","alert('loaded!')")

This causes an alert saying "loaded!" to popup everytime iframe1 finishes loading or reloading its content, and is the equivalent of manually writing:

<iframe src="blah.html" onload="alert('loaded!')">

However, the sad part is that this straightforward technique does not work in IE (as of 6) at all. Instead you must use the proprietary attachEvent() method as follows:

document.getElementById("iframe1").attachEvent("onload",FuncName)

Notice the BIG problem with the above syntax though. You can only pass a function name but cannot specify its parameters! This has vexed me (and I'm sure many others) pretty often in the past. Once you understand how lambdas/anonymous functions work though, the elegant solution becomes obvious:

document.getElementById("iframe1").
attachEvent("onload",function() { alert('loaded!') } )

In the above, Javascript lets us substitute an inline function definition in place of a function identifier. The function is created at this point and exists ('lives') without having to be formally referenced or declared anywhere else. The reason for the term anonymous function now becomes obvious. [ Note: while the above technique will give identical results to the Mozilla method, it is technically not equivalent and involves an additional indirection or at least that's how it seems to me... This is because instead of calling alert() directly, the call lies within an anonymous function which just serves as a dummy wrapper. ]

This sort of power and flexibility that the lambda syntax brings is just impossible in C/C++, Java and other static languages and proves that Javascript does indeed belong in the pantheon of higher order languages along with Python, Ruby, Lisp and others.

3 Comments:

  • Thanks for the post. Here’s a free developer program you can join to increase your knowledge and credibility in cloud application development. Get certified and bid on Caspio-specific projects to earn more money http://blog.caspio.com/news/announcing-caspio-certification-and-developer-network/

    By Anonymous Terry Lawton, at February 02, 2012  

  • They can tweak the percentages depending on how a lot they've paid out vs. how a lot they've taken in. If players do poorly, and it dips too far beneath the proportion, they'll outright pressure wins on you and offer you an unbeatable board where you can to|you possibly can}'t lose and may safely play for the jackpot repeater. If I get free spins or a bonus, I will spin perhaps a couple of occasions after understanding 로스트아크 I will get nothing, then I will change games instantly. You need to be very focused when enjoying in} and be very patient.

    By Anonymous Anonymous, at December 08, 2022  

  • If your 3D printer doesn't use the SOLIDWORKS 3D Print API, you'll be able to|you probably can} still export the file as described above and import it into your printer’s slicer software. We know you could have} a variety of CAD packages and file format choices to choose from|to select from} when designing and sharing your part designs. The STL file, short for stereolithography, is the first CAD file format that’s used for additive manufacturing design tasks. Giro Bike Helmet Men This tip covers method to|tips on how to} successfully design 3D-printed parts for STL. When it involves 3D printing steel, there are individuals who may select to use a laser cutter as an alternative. The cause is that 3D printed tasks using steel can be pricey and time-consuming.

    By Blogger khr87q8obs, at December 22, 2022  

Post a Comment

<< Home