I always say: Twitter Compactly
When creating a reusable javascript library, namespacing can become a problem. It is common now the library is wrapped in a closure, and only one object becomes global. Established libraries like jQuery or Dojo have no issue, because nobody would use their base namespaces (jQuery and dojo respectively).
However, lesser known libraries can get namespace collisions. Below I describe a method to avoid these collisions, without updating the library script. This way, updates of these scripts can be deployed without a problem. First I will discuss the usage of the script. Then I will show the script itself.
When you include it like below, the namespace will be myid:
<script src="myid.js" type="text/javascript"></script>
With the added query, you get the namespace myIdentifier:
<script src="myid.js?name=myIdentifier" type="text/javascript"></script>
With the noConflict-method, you establish the same. When an other library already used the same name, the namespace is restored:
<script type="text/javascript"> myid = { is: function(id) { alert("Helo there, "+id); } }; </script> <script src="myid.js" type="text/javascript"></script> <script type="text/javascript"> myid.is('test'); // --> alert('Hello test') var myIdentifier = myid.noConflict(); myid.is('test'); // --> alert('Hello there, test') myIdentifier.is('test'); // --> alert('Hello test') </script>
The code:
new function() { var name = 'myid'; //default name var scripts = document.getElementsByTagName('SCRIPT'); if(scripts && scripts.length) { var src = scripts[scripts.length-1].src; var x = /(\?|&)name=(.+)($|&)/i.exec(src); if (x && x.length) { name = x[2]; } } var noConflict = window[name]; window[name] = { is: function(id) { alert('Hello '+id); }, noConflict: function() { var result = window[name]; if (noConflict === void 0) delete window[name]; else window[name] = noConflict; return result; } } }
The code above is run at load-time (not at onload). So the last element of the getElementsByTagName-method always returns the correct element. The only disadvantage is the script can’t be loaded with the DEFER attribute.
Nice way of Lazy initialization in C#.
private Item _item; protected Item item { get { return _item ?? (_item = new Item()); } }
I just found out, you can create an alias for a type. This is especially handy for generic collection types.
using System; using SlowArray=System.Collections.Generic.Dictionary<int,string>; class Program { static void Main(string[] args) { SlowArray d = new SlowArray(); d.Add(0, "ing"); Console.WriteLine(d[0]); } }
The using alias directive was introduced in .NET Framework v1.1. However, generics were introduced in v2.0.
I wanted to add categories to my table. For speed and simplicity, I just added five varchar fields to the table: cat0..cat4. Instead of a big OR-expression in my WHERE clause I did this:
SELECT * FROM TABLE WHERE @Category IN (cat0,cat1,cat2,cat3,cat4)
The key insight for me was you can add fields in the comma-seperated list, instead of values. And it’s more readable too. But it has the same execution plan as the “big OR”-variant.