Groups | Blog | Home
all groups > dotnet jscript > july 2006 >

dotnet jscript : JavaScript class creation query


Igor Tandetnik
7/10/2006 12:42:27 PM
[quoted text, click to view]

It's better to define a method like this:

function Timer() {...}
Timer.prototype.UpdateTimer = function() {...};

This way the function is created only once. In your original code, an
instance of the function object is created anew every time an instance
of Timer is created.

[quoted text, click to view]

I've never seen this syntax. In IE, it produces an error " 'Class' is
undefined".

[quoted text, click to view]

All major JavaScript libraries I know of do it this way. See e.g.
http://dojotoolkit.org/

[quoted text, click to view]

Well, #2 does not work. #1 has a nice encapsulated syntax, constructors
with parameters - you can almost pretend you are working in an object
oriented language. #3 is useful for a quick one-off object, but this
approach does not lend itself to reuse. Also, an object created with #1
can be tested with instanceof operator:

var timer = new Timer(0);
if (timer instanceof Timer) {...};

#3 also does not allow one to use prototype property to create a method
just once: you have to define it every time you create an instance,
which adds performance overhead. One can work around it like this, but
it's not as clear as #1:

function Timer_UpdateTimer() {...};
function makeTimer(source) {
var timer = {};

// Class Properties
timer.timerID = 0;
timer.tStart = null;

// Class Methods
timer.UpdateTimer = Timer_UpdateTimer;
}
var timer = makeTimer(0);


Almost everything you can do with #1 you can also do with #3, but I feel
#1 syntax is cleaner. The two things you can only achieve with #1 are
support for instanceof, and inheritance (in JavaScript, you can arrange
for one class to "inherit" from another, several levels deep; multiple
inheritance is not really supported, but there are techniques to fake
it).

[quoted text, click to view]

#1.
--
With best wishes,
Igor Tandetnik

With sufficient thrust, pigs fly just fine. However, this is not
necessarily a good idea. It is hard to be sure where they are going to
land, and it could be dangerous sitting under them as they fly
overhead. -- RFC 1925

Dhruba Bandopadhyay
7/10/2006 5:04:04 PM
I have came across 3 different ways of creating a structure or class.

1. Class as a function way

// Class Constructor
function Timer(Source) {

// Class Properties
this.timerID = 0;
this.tStart = null;

// Class Methods
this.UpdateTimer = function() {
};
}

// Example
var timer = new Timer(0);
timer.UpdateTimer();

2. Class.create & prototype way

var WindowDelegate = Class.create();
WindowDelegate.prototype = {
// Class Constructor
initialize: function() {
},
canClose: function(win) {
}
}

3. Declare as a var way

var myObserver = {
variable: null,
anotherVariable: 33
stringVariable: 'asdf',
aFunction: function(a, b) {
}
}

I assume #3 is just a object/structure instance rather than a class
definition.

Has anyone came across #1 way? What are the differences between these 3
ways?
And what is the proper way to define a class?

Anthony Jones
7/11/2006 12:00:00 AM

[quoted text, click to view]

There are ultimately two 'proper' ways to define a class as you have already
touched on. To clarify:-

Option A.

function MyClass(value)
{
var x = 0, y = 0 //private class level variables

this.a = value // public member

function fnPrivateStuff() // private functions
{
...
}

this.publicStuff = function() // public method
{
...
}

}


This approach provides much stronger encapsulation (x,y for example can only
be referenced from a function defined inside the class) it is therefore more
suited to large complex objects. However as pointed out instances of this
type are more expensive to create so may not be the best choice if a large
number are to be created. A more important point IMO is that implementing a
design that incoporates generalised and specialised classes (inheritance) is
awkward in this approach.


Option B

function MyClass(value)
{
this.x = 0; this.y = 0
this.a = value
}
MyClass.prototype.fnPrivateStuff = function()
{

}
MyClass.prototype.publicStuff = function()
{

}
MyClass.prototype.publicStuff2 = function()
{

}

This approach has much weaker encapsulation. In fact it has no
encapsulation at all. The distinction between public and private members
would have to rely on conventions such as the use of a preceeding underscore
in member names. Creating a large number of instances of this class is
cheaper since the created instance only needs to carry the value the member
variables not the functions. This approach's biggest strength is in
implementing specialisation of general classes:-

function MyDerivedClass(value1, value2)
{
MyClass.call(this, value1) //Base class constructor
this.b = value2
}
MyDerivedClass.prototype = new MyClass()
MyDerivedClass.prototype.somePublicMethod = function()
{

}
MyDerivedClass.prototype.publicStuff2 = function() // override
{

}


On thing often missed in the prototype approach is that default values for
non-function members can also be defined. This further reduces the cost of
creating instances of these classes where member values are only actually
created when a new value is assigned to them. The MyClass above can look
like this:-

function MyClass(value)
{
this.a = value
}
MyClass.prototype.x = 0
MyClass.prototype.y = 0


HTH,

Anthony


Peter Torr (MS)
7/12/2006 7:57:08 AM
[quoted text, click to view]

I recently made two fairly detailed posts about this on my blog:

http://blogs.msdn.com/ptorr/archive/2006/06/13/630208.aspx

http://blogs.msdn.com/ptorr/archive/2006/06/19/638195.aspx

A third is on the way...

[quoted text, click to view]

Tecnically these are not classes and there is no "right" or "wrong" way of
doing it -- if the interpreter doesn't blow up, clearly you've done
something right :-)

Peter

--
Peter Torr - http://blogs.msdn.com/ptorr
HD DVD Program Manager

asdf
7/19/2006 12:00:00 AM
Net neutrality
class free society
school is over (that one was deleted by ms)

We don't want to be "classified" as lusers, users or idiots.
--------------------------------------------
The net as is, is a keeper.

We paid for it for 10 years plus.

We own it.

We shall cause uncomfort to anyone,
tresspassing our ways and means and believes
of the Internet as is.

Never without our consent.

We don't want an iraque modell of conquest
to hush potentil progress other than fuzzy Texan business laws..
--------------
Let the old guys the US Congress know that !

=========================

I am not a Bush fan, but I know when he rides a bike,
he is clear in his mind.

He just does not understand to communicate beyond his
education.



..




[quoted text, click to view]
asdf
7/19/2006 12:00:00 AM
correction

" than fuzzy Texan business laws.."

should be

"hurting fuzzy Texan business laws.."







[quoted text, click to view]
AddThis Social Bookmark Button