Mailing List



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.


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

Add A Comment



Email Address: (not public, used to send notifications on further 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. SaveMySerials - Protect yourself from theft, fire, natural disasters and more by recording your serial numbers