Microsoft Most Valuable Professional

Chris Pietschmann

An MVP From Wisconsin

JavaScript Prototypal Inheritence Explained in Simple Terms

Recently, I started working on a new open source project (you'll have to wait until the first release to find out what it is), and the goal of it is to play nicely with any JavaScript Library you want to use. That means it needs to work nicely with jQuery, prototype, ASP.NET AJAX and the such. This means that I need to grow all my own code for registering namespaces and using inheritence.

One major JavaScript feature that I needed a refresher on, and ultimately get figured out, is Prototypal Inheritence. Sounds fancy, right? Well, it's actually rather simple to do, but can be a little cryptic since there isn't much info out there that just shows you a really basic example of doing it. So, this is why I decided to write this post; to give a really simple example and explanation on JavaScript Prototypal Inheritence.

Basics of Prototypal Inheritence

First, here's the really simple code:

FirstClass = function() {
    /// Constructor
   this.test1 = "Chris";
};
FirstClass.prototype.GetTest1 = function() { return this.test1; };

SecondClass = function() {
    /// Constructor
};
SecondClass.prototype = new FirstClass;
SecondClass.prototype.GetTest1 = function() { return "SecondClass: " + this.test1; };

First, we have the FirstClass object defined. In the constructor we are defining and setting the objects "test1" variable to "Chris". Then a "GetTest1" function is being defined using prototype that returns the value of the "test1" variable. Now, we have a simple objec that will return the test "Chris" when you execute the "GetTest1" method.

Here's an example of using the FirstClass object:

var a = new FirstClass();
alert(a.GetTest1());

Secondly, we have our SecondClass object, that also sets a variable named "test1" and implements a GetTest1 function. This object woud basically be the same, except we want to return a slightly modified string from "GetTest1" instead of returning what FirstClass returns. So, for this we need to use inheritence.

The key to prototypal inheritence is, the line that reads "SecondClass.prototype = new FirstClass". This basically takes a copy of FirstClass, constructor and prototype stuff, and sets SecondClass's prototype to that. Then after we do this, then we redefine the "GetTest1" method to be what we want it to be. And, now when we execute "GetTest1" it will return "SecondClass: Chris" instead of just "Chris".

Here's an example of using the SecondClass object:

var b = new SecondClass();
alert(b.GetTest1());

Now isn't that simple? I told you so.

Calling Base Object Methods

Now, one thing that you're probably used to (especially if you're used to .NET) is being able to access the Base objects version of the methods. In the example above that would be "GetTest1".

To do this, we need to grab a copy of the base objects "GetTest1" method within our SecondClass objects constructor. Then we can use this within our new "GetTest1" method.

Here's an example of doing this:

SecondClass = function() {
    this.base_GetTest1 = FirstClass.prototype.GetTest1;
};
SecondClass.prototype = new FirstClass;
SecondClass.prototype.GetTest1 = function() { return "SecondClass: " + this.base_GetTest1(); };

Using this technique, you can easily copy any logic that the Base object used within the method and reuse it within our new method. This same technique can be used for copying methods over from other objects, not only the one that our object inherits, but if you do this you need to make sure any variables and methods it calls exist in your object.

Currently rated 3.0 by 2 people

  • Currently 3/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Categories: JavaScript
Posted by crpietschmann on Tuesday, September 30, 2008 4:45 PM
Permalink | Comments (1) | Post RSSRSS comment feed


Less Than 36 Hours Left to Enter the Community Coding Contest!

There is only 1 day left to submit your entries for the Community Coding Contest!! There are currently only 5 entries submitted, so your chances are pretty good if you submit an entry today! The deadline to submit entries is 12AM EST October 1st, 2008, that's less than 36 hours away. And, then voting begins.

Submit Your Entry Now!

If you're not familiar with the contest, it's an awesome chance to win a Free MSDN Premium Subscription with Visual Studio 2008 Team Suite, plus a few other prizes!

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:
Categories: General
Posted by crpietschmann on Tuesday, September 30, 2008 12:50 PM
Permalink | Comments (0) | Post RSSRSS comment feed


Virtual Earth 6.2 JavaScript Intellisense Helper Released

Today, "We" released a new updated version of the Virtual Earth JavaScript Intellisense Helper that is updated to support the brand new Virtual Earth v6.2. You can learn more about what's new in Virtual Earth v6.2 here: http://blogs.msdn.com/virtualearth/archive/2008/09/24/announcing-the-virtual-earth-web-service-and-virtual-earth-map-control-6-2.aspx

It appears that Mark beat me to blogging this release (which makes sense since he's the one that posted the final ZIP of the release): http://blogs.msdn.com/devkeydet/archive/2008/09/30/released-virtual-earth-javascript-intellisense-helper-for-6-2.aspx

And, Thanks for the props Mark!

In case you aren't familiar with what the "Virtual Earth JavaScript Intellisense Helper" is:

"The purpose of this project is to fully enable JavaScript Intellisense for the Virtual Earth Map Control inside of Visual Studio 2008.

Creating Microsoft Virtual Earth mashups and applications just got a whole lot easier. This JavaScript library enables Intellisense for the Microsoft Virtual Earth 6.2 (current release) AJAX control in Visual Studio 2008"


Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Posted by crpietschmann on Monday, September 29, 2008 8:30 PM
Permalink | Comments (0) | Post RSSRSS comment feed


Silverlight Isolated Storage File/Directory Location

I was just building something in Silverlight that makes use of the Isolated Storage, and I uncovered a bug in Silverlight 2 Beta 2 that cause's it to crash and you are unable to write to the Isolated Storage. So, I dug in and found the location on disk where Silverlight stores the files for Isolated Storage.

Here's the file location under Windows Vista where Silverlight stores the files for Isolated Storage:

C:\Users\{username}\AppData\LocalLow\Microsoft\Silverlight\is

Just replace the text "{username}" with the Windows Username of the user the data is stored for.

The bug that I ran into was also causing the Silverlight Configuration dialog (which pops up when you right click on Silverlight content within the browser) to crash when I selected the Application Storage tab. So, this bug prevented me from clearing the cache, and I wasn't able to get my app working again. I just deleted all the files within the above directory and if fixed my problem.

If I figure out what I did to cause the issue, I'll find the place to report the bug.

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Categories: Silverlight
Posted by crpietschmann on Monday, September 29, 2008 4:33 PM
Permalink | Comments (0) | Post RSSRSS comment feed

Windows vNext (Windows 7?) Feature Ideas

Here are some of my thoughts on the Windows operating system and how thing could be improved in the "next" version after Vista. Also, the ideas aren't in any particular order of importance, they are just in the order I typed them in.

1) Make the Desktop version of Windows more Modular, like Windows Server 2008

Windows Server 2008 is more modular, you can install Server Core that doesn't even have a graphical interface, or you can install Windows Server 2008 with just the features you want/need. Why not make a Desktop version of the OS like that? Let the advanced users, during installation or from the Programs/Features Dialog, decide which features they want to install. Currently, Windows Vista Ultimate could be considered a little bloated, and the extra features installed that I don't use are just taking up space, and some run in memory affecting my system performance. For instance, If I don't want or use Windows Media Center, then don't install it, it's not required for Windows to run.

2) Improve Data CD/DVD Burning, make it usable

Since WinXP, we've had Data CD burning capabilities. It hasn't been the best functioning, and IMO is too slow to use. I actually use a Free burning program called Deep Burner to burn data discs, and its' performance is much faster. This is an area in Windows that needs improvement, and since the functionality is already baked into Windows, why not improve it to be the best it can be?

3) Add Mount ISO and Burn ISO Support

Developers and Sys Admins get ISO images from their MSDN subscriptions and elsewhere quite often, so why not make it easier on us by adding the capabilities to 1) Mount an ISO image to a Virtual Drive, and 2) Add support for burning ISO images to Disk. These features would make things much simpler, and such often used functionality should just be baked into the OS.

4) Add Blu-Ray Burning Support

Lets face it, our backup needs are growing in size. When I backup my data I need to use multiple writable DVD's to back everything up. Soon enough, Blu-Ray burners and media will drop in price enough to make them practical to use for backups. Since that will happen in the next couple years, why not add support for it to the OS?

5) Custom Themes and Visual Styles

It's nice to have a consistent look and feel for Windows, especially for branding. But, why not add customizability to Windows for users to design and use their own Visual Styles and Themes? It would make Windows more enjoyable to use, since you could change the look of it to match your personality and preference. Or, alternatively a bunch more (like 10 or more) Visual Styles could be baked into Windows for the users to choose from. This feature idea is all about personality.

6) Decouple Internet Explorer from the OS

It sure would be nice if you could have multiple versions of Internet Explorer installed on the same machine, at least from a developer/designer perspective. By decoupling IE from the OS, it would also enable you to uninstall Internet Explorer completely if you choose. Give a little more freedom to the user as to which web browser's they want to use and install.

This also goes along with the ability for full debugging of ASP.NET web applications within other non-IE browsers from Visual Studio, but this is really a seperate idea that's more a suggestion for the next version of Visual Studio.

7) Integrate Hyper-V into the Desktop Version of Windows

Virtualization is a technology that is not only valuable for Server Virtualization, but also for Desktop Virtualization for Developers. Yes, you can install Windows Server 2008 on your development machine and then use Hyper-V for virtualizing dev/test environments, but if you don't have an MSDN Subscription the Windows Server 2008 license can be expensive. Why not add Hyper-V to the Desktop Version of Windows, so developers can utilize it on their workstations and completely get rid of Virtual PC 2007?? Also, it sure would be nice to use Hyper-V on the same machine that I use Media Center on... After all, I use one pc for EVERYTHING!

8) Add Basic Virus Scanner

Let's face it, almost every pc has some kind of Virus scanner installed. Windows already has a Spyware scanner called Windows Defender. Being an advanced user and developer, I just don't get spyware on my machine, but the average user does. A worse thing that the average user also gets are Virus's, and they are much worse than Spyware. Why not add a new Virus scanning tool to the next version of Windows? After all, it will help make it an even more secure OS, right? One critical thing with this is, give the option to disable or completely uninstall the Virus scanner for those who want to use a different one, or none at all.

Here's a little secret, I haven't had a Virus scanner installed on my machine in over 2 years. I am very carefull about what I download/run, and have learned to be this way from getting burned over the years. I also do not download warez, which from my experience is the same as unprotected sex with a complete stranger. I have a second machine that I let anyone that comes over use, and it doesn't have a Virus scanner also, but I don't let anyone run as Administrator on that box either. I could argue that you don't need a virus scanner, but the truth is most user (the average computer user) does!

9) More Sidebar Plugins Baked into Windows

There need to be more Sidebar plugins baked into Windows.  Yes, you can go online and download more, but why not have Windows come with more usefull ones? For example, I was initially surprised that Vista didn't come with Media Player or Live Messenger Sidebar Plugins.

10) Isn't it about time MS Paint gets an overhaul??

The functionality in MS Paint hasn't changed since Windows 95! There's a Free app called Paint.NET that is just plain awesome! Why not integrate a nicer image editing application into Windows?

 

 

UPDATED: 9-15-2008 - I added another feature idea to the list....

 

11) Be able to ReOrder Windows in the Taskbar by drag-and-drop

It sure would be nice if you could reorder your task list in the Taskbar by using drag-and-drop to change their order. The automatic ordering of the windows/applications along with its automatic grouping by application is a bit counter productive.



Ok, there's the first 10 things I could think of at the moment of how to improve Windows. If you have any other ideas, please post a comment, I'd be interesting in hearing them.

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:
Categories: General
Posted by crpietschmann on Friday, September 05, 2008 10:42 AM
Permalink | Comments (2) | Post RSSRSS comment feed

About the author

I'm Chris Pietschmann, go to the About Me page to learn more about me.

Search

Sponsors

Web.Maps.VE - ASP.NET AJAX Virtual Earth Mapping Server Control

Recent comments

Disclaimer


This work is licensed under a Creative Commons Attribution 3.0 United States License, unless explicitly stated otherwise within the posted content.
© Copyright 2004 - 2009 Chris Pietschmann