posts - 218, comments - 222, trackbacks - 68

My Links

News




I am a Microsoft Certified Application Developer MCAD Chartered Member (C# .Net) and born in Bangladesh.
I work for Ocean Informatics Pty Ltd as a Senior Developer - Analyst.
I am also co-founder and core developer of Pageflakes (acquired by LiveUniverse) www.pageflakes.com
and most recently created SmartCodeGenerator

My Articles
Flexible and Plugin based .Net Application..
Mass Emailing Functionality with C#, .NET 2.0, and Microsoft® SQL Server 2005 Service Broker'
Write your own Code Generator or Template Engine in .NET
Smart Code Generator .NET: Usage Overview
Smart Code Generator .NET: Architectural Overview
Smart Code Generator .NET: using with NAnt and Cassini

Archives

Free Programming Language Training

Javascript Tips: Carefully use "this" when writing classes, else you may cause memory leak.

Lets say we want to declare a class in Javascript, which is equivalent to the following C# class.

public class Student
{
  public string FirstName = "";
  public string LastName = "";

  public Student( string firstname, string lastname)
  {
    this.FirstName = firstname;
    this.LastName = lastname;
  }
  
  public string GetFullName()
  {
    return FirstName + LastName;
  }
}

To write a similar class in JavaScript we can do something like the following [ but this will create memory leak, I am explaining that in a moment ]

function Student ( firstname, lastname)
{
  this.FirstName = firstname;
  this.LastName = lastname;

  this.GetFullName = function()
  {
    return this.FirstName + this.LastName;
  }

}


now in C# if we want to instantiate an object of Student and want to call the GetFullName() method, we do the following.
Student student = new Student("Shahed", "Khan");
string fullname = student.GetFullName();


and we can create as many objects as we want and call its methods, each of the object will maintain its own state, and all objects will use the same copy of the GetFullName()  method.

But Javascript has different behaviour when we do the following on the above Javascript class.

var student = new Student("Shahed","Khan");
car fullname = student.GetFullName();


In Javascript, functions are treated as variables as a result when we create a new object of Student it creates  new sets of firstname, lastname and also a new copy of GetFullname method, as a result we are creating memoryleak.

Do not worry too much, there is a workaround for this, lets redefine the class in a different way.

function Student ( firstname, lastname)
{
  this.FirstName = firstname;
  this.LastName = lastname;

  this.GetFullName = GetFullName;

}

function GetFullName()
{
  return this.FirstName + this.LastName;
}

Notice I have moved the GetFullName function out of the class, and for this tweaking all new objects of the Student class will share the same instance of of GetFullName method and avoid memory leak.

Thank you for being with me so far.

Updated 24th Feb
===============
Laurent from Galasoft gave some good feedback, 
JavaScript object oriented should be done by modifying the prototype property of the object, and never by storing methods using the "this" keyword. The workaround provided above is not good practice, as it forces the use of a global function. We should always declare methods in JavaScript object like this:
 
function Student(firstName, lastName)
{
 this.firstName = firstName;
 this.lastName = lastName;
}

Student.prototype =
{
 getFullName : function()
 {
   return this.firstName + " " + this.lastName;
 }
}

also note correct naming convension, ( Javascript follows Java notation not C#). For JavaScript best practices please refer to the work of Microsoft Silverlight team.

Print | posted on Thursday, February 21, 2008 5:38 PM |

Feedback

Gravatar

# re: Javascript Tips: Carefully use "this" when writing classes, else you may cause memory leak.

Hi,

JavaScript object oriented should be done by modifying the prototype property of the object, and never by storing methods using the "this" keyword. The workaround you give is not good practice, as it forces the use of a global function. You should always declare methods in JavaScript object like this:

function Student(firstName, lastName)
{
this.firstName = firstName;
this.lastName = lastName;
}

Student.prototype =
{
getFullName : function()
{
return this.firstName + " " + this.lastName;
}
}

This way, you have only one cleanly declared copy of getFullName. JavaScript is not C#, its object-oriented patterns are different.

Note also that I corrected the casing of your methods. JavaScript uses the Java notation guidelines for methods, not the C# one.

If you're interested to learn more about JavaScript, you can check the work of the Silverlight team at Microsoft, they do a great job applying best practices in JavaScript.

HTH,
Laurent
2/23/2008 10:43 AM | Laurent

Post Comment

Title  
Name  
Email
Url
Comment   
Please add 4 and 8 and type the answer here:

Powered by: