[javascript-dev] Re: Classes/inheritance in javascript?
Adam Murray
adamjmurray at gmail.com
Wed Jan 9 00:08:28 MST 2008
- Previous message: [javascript-dev] Re: Re: gl orientation and "moving forward"
- Next message: [javascript-dev] creating jit.matrix in javascript...
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Hi Anthony,
You can do this in javascript but it is pretty strange. Here's an example:
-----
function Class() {
this.member = "a member variable";
}
Class.prototype.method = function() {
post(this.member + "
");
}
instance = new Class();
instance.method();
----
Javascript is a prototype-based langauge rather than a standard OO language. This means you define a constructor as a function, and attach methods to the class by assigning them to the constructor function's prototype property.
Many attempts have been made to simulate standard OO inheritance in javascript. If that is something you are interested take a look at this:
http://www.sitepoint.com/blogs/2006/01/17/javascript-inheritance/
I am not recommending this person's approach, but the blog post is good because it provides lots of links and discussions to alternate approaches. One of them may suit you.
If you want to learn JavaScript better, I highly recommend the book "JavaScript The Definitive Guide" published by O'Reilly. It is by far the best javascript reference I have encountered. No online resources come close.
-Adam
Quote: Anthony Palomba wrote on Tue, 08 January 2008 10:13
----------------------------------------------------
> I was wondering if there is a way to declare classes in javascript
> and instantiate them? Does anyone have an example that demonstrates this?
>
>
> Thanks,
> Anthony
----------------------------------------------------
- Previous message: [javascript-dev] Re: Re: gl orientation and "moving forward"
- Next message: [javascript-dev] creating jit.matrix in javascript...
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
