These ARE probably THE most important questions regarding JavaScript and
oop.
I think the O'Reilly book by Flanagan is the best JavaScript reference
manual; the oop discussion is brief but good and accurate. Anyone have a
better one???? ... that said,
Question 2 comes first ... you're asking the mechanism of JavaScript objects
and inheritance.
Question 1 is more specific on the use of prototype for inheritance.

2) class 'foo' is a constructor function(and a few other things but not
much). It is inheriting from 'DynLayer'. You are calling 'this.DynLayer()'
to simply call the constructor of the parent. Why is it saved in 'this'?...
Using 'this.DynLayer()' allows the 'DynLayer()' constructor function to
access the instance variables contained in the 'foo' class...The reason
'DynLayer()' is turned into a method of the 'foo()' class is that methods
are able
to use the 'this' variable of the object that was created.  Instance methods
can use 'this'.
So when you say 'new foo(x,y,z)' the code in 'DynLayer()' is executed.

1)
In order for 'foo' to be an class it uses a 'prototype' object to inherit
properties.  That's what a prototype property (of the constructor function)
does.  When you say 'alert(this.abc)'  inside of a foo method, if there is
not 'abc' in 'foo', then JavaScript looks at 'foo.prototype' and uses it's
'abc' if it has one. Therefore 'abc' is inherited from the superClass of
'foo' which is 'DynLayer'.  This is done invisibly.  If you write to abc
like this:
this.abc = "hello" inside of a foo method, then this does not happen, the
instance variable 'abc' is created in 'foo' not in the superClass. Now a
subClass of 'foo' can access 'abc'...How? the subClass set it's prototype to
be 'new foo()' creating a version of 'foo' that has the fields to be
inherited using this prototype technique.
It is confusing but there's only one prototype and many 'this' objects, so
any number of 'foo' objects can use the methods and data in the 'prototype'
object but if they write to a field of 'this' such as 'this.abc = 5' they
create a new version of 'abc'. This makes the whole thing more efficient.


=====******

I wonder if it would work to change it thus:
 function foo(x,y,z)
 {
       // this.DynLayer = DynLayer; --->REMOVE THIS
     this.DynLayer();
 }
foo.prototype = new DynLayer()
foo.prototype.DynLayer = DynLayer;   // ---> ADD THIS

=====******
Any good programmers out there??? This should be more efficient!!!
Yoe!
Todd.



----- Original Message -----
From: "James Musick" <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Monday, August 26, 2002 1:21 PM
Subject: [Dynapi-Help] What do these lines of code mean/do?


> function foo(x,y,z)
> {
>     this.DynLayer = DynLayer;
>     this.DynLayer();
> }
> foo.prototype = new DynLayer()
>
>   I've seen this type of thing in a couple examples (as part of the object
> definition).
>   1) what does it mean to do a foo.prototype? I understand when you do
> something like
> foo.prototype.isCool=true           or
> foo.prototype.setColor=function(col)
> but not the one that isn't 'adding' some object/function to the object
> prototype.
>   2) the first 2 lines....
> I'm guessing that you instantiated the object by saying new
> foo(x,y,z)...this somehow created made the object a DynLayer because of
the
> foo.prototype...Then since it's a DynLayer you're pointing the
this.DynLayer
> property to the DynLayer function and then initializing it with the
> this.DynLayer() function call???
>   That doesn't make much sense...so any clarification would be great,
> thanks!
>
>   -James
>
> _________________________________________________________________
> Join the world's largest e-mail service with MSN Hotmail.
> http://www.hotmail.com
>
>
>
> -------------------------------------------------------
> This sf.net email is sponsored by: OSDN - Tired of that same old
> cell phone?  Get a new here for FREE!
> https://www.inphonic.com/r.asp?r=sourceforge1&refcode1=vs3390
> _______________________________________________
> Dynapi-Help mailing list
> [EMAIL PROTECTED]
> https://lists.sourceforge.net/lists/listinfo/dynapi-help



-------------------------------------------------------
This sf.net email is sponsored by: OSDN - Tired of that same old
cell phone?  Get a new here for FREE!
https://www.inphonic.com/r.asp?r=sourceforge1&refcode1=vs3390
_______________________________________________
Dynapi-Help mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/dynapi-help

Reply via email to