Code sniplets – part I
I do collect sniplets of code. I have them all over the place. Almost every time I read about or bump into a technique, pattern, tests or new way of doing things I find useful of interesting I tend to test it out on my own. Mostly for my own curiosity but also because I learn and understand the concepts easier this way. These sniplets are small isolated bits of test code with comments more or less meant only for my self. Lately I've had some time to start to organize some of those sniplets and write a bit more out filling comments and tests on them than I usually do. Here are the first couple of sniplets.
Classical inheritance in JavaScript
JavaScript is a prototype based language. It's inheritance are done trough the prototype but it's also possible to do classical inheritance. This help function provides classical inheritance to JavaScript.
Interface in JavaScript
JavaScript has no native functionality which provides interfaces on objects. This helper provides a method for using interfaces in JavaScript.
Chaining methods in JavaScript
Chaining - or cascading - is a way of executing methods on objects in a chain. If one need to perform many operations on objects, chaining can save a lot of code. This example displays how chaining are implemented and used.
Sorting an array of objects by the objects attributes in JavaScript
This function is intended to be passed on to the Array.sort(); method and makes it possible to sort an array of objects by the value of attributes in the objects.
Duff's Device implemented in JavaScript
Looping are in itself an expensive process. A for loop is slower than a while loop and so on. Duff's Device is a technique for reducing the number of loops and therefore enhancing the loop procedure. Duff's Device loops about 50% to 70% faster than a traditional loop. This example displays how Duff's Device can be implemented in JavaScript.
NOTE: I do not take any credit for any of the patterns, techniques or solutions. You will find loads of other places describing and addressing the same issues. Some code can even be identical as found other places. These are small tests I've written and played with purely for my own enjoyment after picking up the issue somewhere. I've tried as far as I can to credit each sniplet where I find a reason to do so.
31.10.2009 06:12 - Posted by Trygve - Comments: 0 - Technical
About:
My name is Trygve Lie. I live in Oslo, Norway where I deal with web technology. You can read a bit more about me here.
Search:
Categories:
Links:
Feeds:
Tags
- atom
- blog
- components
- css
- dom
- entitys
- esi
- html
- ie6
- ie7
- ie8
- java
- javascript
- jsp
- jstl
- performance
- roller
- saxon
- scriptless
- selectors
- structure
- theme
- validation
- varnish
- webslices
- welcome
- xalan
- xhtml
- xml
- xslt
Comments: