Classes/inheritance in javascript?

Jan 8, 2008 at 6:13pm

Classes/inheritance in javascript?

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

#35269
Jan 8, 2008 at 6:26pm

Hi see the thread “rendering multiple gridshapes w/ openGL in js” in
the javascript-dev forum which is probably a better place to ask this
question.

best,
wes

On Jan 8, 2008 10:13 AM, Anthony Palomba wrote:
>
> 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
>

#120050
Jan 8, 2008 at 9:06pm

moving this topic to the javascript forum

#120051
Jan 9, 2008 at 7:08am

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 + “n”);
}

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
—————————————————-

#120052

You must be logged in to reply to this topic.