JavaScript local alias pattern

Posted by Bertrand Le Roy on ASP.net Weblogs See other posts from ASP.net Weblogs or by Bertrand Le Roy
Published on Tue, 18 May 2010 00:48:37 GMT Indexed on 2010/05/18 0:51 UTC
Read the original article Hit count: 525

(c) Bertrand Le Roy 2005 Here’s a little pattern that is fairly common from JavaScript developers but that is not very well known from C# developers or people doing only occasional JavaScript development.

In C#, you can use a “using” directive to create aliases of namespaces or bring them to the global scope:

namespace Fluent.IO {
    using System;
    using System.Collections;
    using SystemIO = System.IO;

In JavaScript, the only scoping construct there is is the function, but it can also be used as a local aliasing device, just like the above using directive:

(function($, dv) {
    $("#foo").doSomething();
    var a = new dv("#bar");
})(jQuery, Sys.UI.DataView);

This piece of code is making the jQuery object accessible using the $ alias throughout the code that lives inside of the function, without polluting the global scope with another variable.

The benefit is even bigger for the dv alias which stands here for Sys.UI.DataView: think of the reduction in file size if you use that one a lot or about how much less you’ll have to type…

I’ve taken the habit of putting almost all of my code, even page-specific code, inside one of those closures, not just because it keeps the global scope clean but mostly because of that handy aliasing capability.

© ASP.net Weblogs or respective owner

JavaScript local alias pattern

Posted on Dot net Slackers See other posts from Dot net Slackers
Published on Tue, 18 May 2010 00:00:00 GMT Indexed on 2010/05/18 14:11 UTC
Read the original article Hit count: 525

Filed under:
Heres a little pattern that is fairly common from JavaScript developers but that is not very well known from C# developers or people doing only occasional JavaScript development. In C#, you can use a using directive to create aliases of namespaces or bring them to the global scope: namespace Fluent.IO { using System; using System.Collections; using SystemIO = System.IO; In JavaScript, the only scoping construct there is is the function, but it can also be used as a local aliasing...

Did you know that DotNetSlackers also publishes .net articles written by top known .net Authors? We already have over 80 articles in several categories including Silverlight. Take a look: here.



Email this Article

© Dot net Slackers or respective owner

JavaScript local alias pattern

Posted by Latest Microsoft Blogs on ASP.net Weblogs See other posts from ASP.net Weblogs or by Latest Microsoft Blogs
Published on Tue, 18 May 2010 00:48:00 GMT Indexed on 2010/05/18 2:31 UTC
Read the original article Hit count: 525

Here’s a little pattern that is fairly common from JavaScript developers but that is not very well known from C# developers or people doing only occasional JavaScript development. In C#, you can use a “using” directive to create aliases of namespaces Read More......(read more)

© ASP.net Weblogs or respective owner

JavaScript local alias pattern

Posted on Dot net Slackers See other posts from Dot net Slackers
Published on Tue, 18 May 2010 00:00:00 GMT Indexed on 2010/05/18 2:51 UTC
Read the original article Hit count: 525

Filed under:
Heres a little pattern that is fairly common from JavaScript developers but that is not very well known from C# developers or people doing only occasional JavaScript development. In C#, you can use a using directive to create aliases of namespaces or bring them to the global scope: namespace Fluent.IO { using System; using System.Collections; using SystemIO = System.IO; In JavaScript, the only scoping construct there is is the function, but it can also be used as a local aliasing...

Did you know that DotNetSlackers also publishes .net articles written by top known .net Authors? We already have over 80 articles in several categories including Silverlight. Take a look: here.



Email this Article

© Dot net Slackers or respective owner

Related posts about ASP.NET

Related posts about c#