Hi, folks. I just write Ox.Class, which provides a convenient way to
write OO in JavaScript, and it supports multiple inheritance, too. You
may get the code at http://pygments.org/demo/485/ , Ox.Class is based
on MochiKit, however, it''s not difficult to replace the MochiKit
functions to
Prototype functions (and that functions are not much). I''ve seen the
Class implement resident in Prototype, but I think if you can get
some
ideas from Ox.Class, too. :) Here''s my brief tutorial:
1. Define the class:
Person = Ox.Class(object, {
''__init__'': function (name) {
this.name = name
},
''say'': function (msg) {
return "I''m " + this.name + ''.
'' + msg
}
})
This is an Ox.Class-type class. Actually, the basic declaration of
an
empty class of this type is Ox.Class(object), the object is a basic
class defined by Ox.Class. All class you define should inherit the
''object'' class, although it''s still ok if you
don''t. There is an
''object''
class for inheriting because Ox.Class is inspired by "Python"
programming
language. As you can see next, I defined a ''__init__''
method, it
acts as
a constructor of this class. Ox.Class uses the name
''__init__'' is
followed
by Python convention, too.
2. Create an object instance:
john = new Person(''John'') // just like how it should be
in
JavaScript
john.say(''Hello'') // should return "I''m
John, Hello."
3. Inheritance
Pirate = Ox.Class(Person, {
''say'': function (msg) {
return "I''m " + this.name '', a pirate.
'' + msg
}
})
Now I defined a class inherits from Person, we pop up "object"
because
Person already inherts object, so in theory object is Pirate''s
parent''s parent. Now see what effects.
hook = new Pirate("Hook")
hook.say(''How are you?'') // return "I''m
Hook, a pirate. How are
you?"
4. Multiple Inheritance
Ox.Class also provides multiple inheritance that JavaScript
essentially
not support. First, I defined another class:
Gender = Ox.Class(object, {
''__init__'': function () {
this.gender = ''female''
}
})
Then defined a class which inherits two class, Person and Gender:
Girl = Ox.Class(Person, Gender, {
''say'': function () {
return "My name is " + this.name + ", I''m
" + this.gender
}
})
See? That''s pretty simple. You just need to put all the classes you
want
to inherit before your methods. All things you need to know is that
classes
which on left side have higher priority than right side. In this
case,
Person has higher priority than Gender.
sheri = new Girl(''Sheri'')
sheri.say() // return "My name is Sheri, I''m female"
5. Use Parent''s method
It is possible to use parent''s method. To achieve this, we need to
know
where the method is and in which class. For example, we may modify
the
Pirate class to:
Pirate = Ox.Class(Person, {
''say'': function (msg) {
return Person.prototype.say.apply(this, arguments) +
" I''m a pirate."
}
}
hook = new Pirate()
hook.say(''Hello.'') // should return "I''m
Hook. Hello. I''m a
pirate."
It''s a little tricky. We use the JavaScript''s built-in
"apply"
method
to pass our arguments to the parent''s method. keep in mind one
thing,
for referring to the method in class, we need to specify the
''prototype''
between the class name and the method name. Don''t forget JavaScript
is
a prototype-based OO programing language.
In Ox.Class, it supports to use "pt" to replace the long word
"prototype".
In this case, we may change the example line to:
return Person.pt.say.apply(this, arguments) + " I''m a
pirate."
It''s still not clear, may I''ll have a better implement in
feature
release. However, Hope Ox.Class helps you. :)
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Spinoffs" group.
To post to this group, send email to
rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-spinoffs?hl=en
-~----------~----~----~----~------~----~------~--~---