NerdyHearn
Home
Blog

Contact
Mailing List

Software

Active Directory Products
Object Compare
Permission Compare

IPhone Products
Calls To Calendar
SMS To CSV
SMS To Gmail
Voicemail To Gmail

Sites
DocuTerminal
How Long For Me
My Music To Me
SaveMySerials
TypeCount

Blog
Twitter

NerdyHearn - Blog


<< Back To All Blogs

Javascript Classes: Comparing C# Instantiated and Static classes to Javascript

Thursday, March 27th, 2008

I see a lot of questions around some of the popular javascript sites about using namespacing in javascript, as well as how to handle static methods while using namespacing. To make an easy example, I am going to give a quick comparison between C# classes and Javascript classes.

In C#, a basic namespacing scheme would be as follows:

namespace NerdyHearn.Com
{
public class MyClass {
...
// This is a method that requires the class to be instantiated
public string MyInsantiatedMethod() { ... }

// This is a static method that does not require instantiation
public static string MyStaticMethod() { ... }
}
}

To use the two methods above, one instantiated, and one static, you would do the following:

// The non-static method requires the class to be instantiated
NerdyHearn.Com.MyClass myClass = new NerdyHearn.Com.MyClass();
string instantiatedResult = myClass.MyInstantiatedMethod();

// The static method does not require instantiation
string staticResult = NerdyHearn.Com.MyClass.MyStaticMethod();

The same declaration in Javascript would be as follows:

// This is the identical non-static method in Javascript
NerdyHearn.Com.MyClass.MyInstantiatedMethod = function() {
...
}

// This is the identical static method in Javascript
NerdyHearn.Com.MyClass = {
MyStaticMethod: function() { ... }
}

To reference both of the functions in Javascript the following is required:

For the non-static method
var myClass = new NerdyHearn.Com.MyClass();
var instantiatedResult = myClass.MyInstantiatedMethod();

For the static method
var staticResult = NerdyHearn.Com.MyClass.MyStaticMethod();

Hopefully that helps some of you to understand the differences between static and non-static methods in Javascript.

Javascripting Tom Out.

Comments

jQuery Howto said on Thursday, January 29th, 2009 @ 8:51 AM

Great article. Here is an article on how to use it with the jQuery object. In other words namespasing with the jQuery http://jquery-howto.blogspot.com/2009/01/namespace-your-javascript-function-and.html

Add A Comment

Name:


URL:


Email Address: (not public, used to send notifications on further comments)


Comments:



Enter the text above, except for the 1st and last character:


NerdyHearn - Latest tech news relating to C#, ASP.NET, SharePoint, PHP, general development, and more. DocuTerminal - Online Filing Cabinet solution. Scan, search and archive your paper documents. SaveMySerials - Protect yourself from theft, fire, natural disasters and more by recording your serial numbers My Music To Me - Stream your subsonic music collection to your Sonos wireless system TypeCount - Count how often you type across different computers! ServrTrackr - Monitor your SSL certificates, website uptime, and DNS configurations