By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
426,034 Members | 1,714 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 426,034 IT Pros & Developers. It's quick & easy.

Prototype Object.extend(new Base() | Hash | Hash.prototype) usage:

P: n/a
I've noticed Object.extend used in a few different ways and I'm having
trouble distinguishing why certain usages apply to a given situation.
>From 1.5.0 prototype.js
[1] On line 804 Ajax.Base is defined as follows:

Ajax.Base = function() {};
Ajax.Base.prototype = {
setOptions: function(options) { <...>

[2] Now later on line 821:

Ajax.Request = Class.create();
<...>
Ajax.Request.prototype = Object.extend(new Ajax.Base(), {
_complete: false, <...>

[3] Preceding that on lines 636 and 664 respectively we have:
Object.extend(Hash, {
toQueryString: function(obj) {

Object.extend(Hash.prototype, {
_each: function(iterator) {
for (var key in this) { <...>
[4] on line 664
var Enumerable = {
each: function(iterator) {
<...>
Object.extend(Enumerable, {
map: Enumerable.collect,

[1] Seems to be going about things in a traditional manner. No class
create(). What exactly is the point of class.create()? It seems highly
redundant.

[2] There's the assignment operator and Object.extend. Would
Object.extend(Ajax.Request.prototype,new Base());
Object.extend(Ajax.Request.prototype, { ... } );
do the same thing?

[3] I'm not sure when / when not to use the prototype keyword ...
actually I don't know what it's for at all! In this example it does it
both ways. What's the difference? Similar situation with [4] and the
illustratory alternative I presented in [2]. Would Ajax.Request
( without .prototype) be correct? Or would that be broken somehow? I
wasted some time experimenting in Firefox with this but Javascript is
pretty fluffy and things like this tend to slip by ~kind of~ working.

Clarification would be greatly appreciated.

Mar 27 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a
On Mar 27, 12:48 pm, "jacob...@gmail.com" <jacob...@gmail.comwrote:
I've noticed Object.extend used in a few different ways and I'm having
trouble distinguishing why certain usages apply to a given situation.
From 1.5.0 prototype.js
You're lost :-)

Prototype.js help is on the Ruby on Rails spinoffs group:

news:rubyonrails-spinoffs
<URL: http://groups.google.com.au/group/ru...pinoffs?lnk=li >
--
Rob

Mar 27 '07 #2

P: n/a
On Mar 27, 12:48 pm, "jacob...@gmail.com" <jacob...@gmail.comwrote:
I've noticed Object.extend used in a few different ways and I'm having
trouble distinguishing why certain usages apply to a given situation.
In my first reply I refrained from further comment, however on reading
your post again there are some sections that deserve comment here.

Firstly, I hope you understand the difference between the Prototype.js
library that is written in javascript and intended to be used with the
Ruby on Rails environment, and the javascript prototype property of
objects.

It is a very poorly named library.
From 1.5.0 prototype.js
To help avoid confusion with the javascript prototype property, the
library is normally referenced as "Prototype.js".
>
[1] On line 804 Ajax.Base is defined as follows:

Ajax.Base = function() {};
Ajax.Base.prototype = {
setOptions: function(options) { <...>

[2] Now later on line 821:

Ajax.Request = Class.create();
<...>
Ajax.Request.prototype = Object.extend(new Ajax.Base(), {
_complete: false, <...>

[3] Preceding that on lines 636 and 664 respectively we have:
Object.extend(Hash, {
toQueryString: function(obj) {

Object.extend(Hash.prototype, {
_each: function(iterator) {
for (var key in this) { <...>

[4] on line 664
var Enumerable = {
each: function(iterator) {
<...>
Object.extend(Enumerable, {
map: Enumerable.collect,

[1] Seems to be going about things in a traditional manner. No class
create(). What exactly is the point of class.create()? It seems highly
redundant.
That is definitely for the Prototype.js news group - I'd like to know
the answer myself. The "official" Prototype.js documentation on
Class.create is at:

<URL: http://www.prototypejs.org/api/class >

and says:

Prototype's object for class-based OOP.

Currently, inheritance is handled through Object.extend.
create

create() -Function

Returns an function that acts like a Ruby class."

>
[2] There's the assignment operator and Object.extend. Would
Object.extend(Ajax.Request.prototype,new Base());
Object.extend(Ajax.Request.prototype, { ... } );
do the same thing?
The documentation says:

extend

Object.extend(dest, src) -alteredDest

Copies all properties from the source to the destination object. Used
by Prototype to simulate inheritance (rather statically) by copying to
prototypes.
[3] I'm not sure when / when not to use the prototype keyword ...
actually I don't know what it's for at all!
Then best you learn - it is fundamental to the object-oriented aspects
of the language. Try the following two articles, take your time
getting through them:

<URL: http://www.litotes.demon.co.uk/js_in...te_static.html >

<URL: http://www.jibbering.com/faq/faq_notes/closures.html >

In this example it does it
both ways. What's the difference? Similar situation with [4] and the
illustratory alternative I presented in [2]. Would Ajax.Request
( without .prototype) be correct? Or would that be broken somehow? I
wasted some time experimenting in Firefox with this but Javascript is
pretty fluffy and things like this tend to slip by ~kind of~ working.
Javscript, or more strictly ECMAScript Language, is not "fluffy" at
all. It adheres to a public specification that, while often
criticised as difficult to read and understand, is pretty solid in
most respects. I fear that your confusion is caused by using
Prototype.js and not understanding what or why things are happening -
there are about 3,000 lines of code to get through.

While some aspects of the javascript language have been criticised
(maybe rightly so, I'm certainly not qualified to judge either way),
its prototype-based inheritance is universally viewed as one of its
great strengths.

However, prototype-based inheritance can be difficult for those
schooled in classic OO programming to come to terms with. My only
advice there is to stop thinking of javascript in terms of classic OO
concepts (stuff like "Class.create" when javascript has no classes)
and approach prototype-based inheritance as a brand new concept.

Clarification would be greatly appreciated.
I'm not sure I've helped, but here's hoping. :-)
--
Rob

Mar 27 '07 #3

P: n/a
Thank you Rob. I'll read those links regarding the prototype keyword
(I was aware it had nothing to do with the prototype.js library - I'm
ignorant but not quite ~that~ ignorant ;). Wasn't aware there was a
prototype group either so thanks for the heads up!

Mar 27 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.