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

J

jacobstr

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.
 
R

RobG

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.

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_info/private_static.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. :)
 
J

jacobstr

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!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,989
Messages
2,570,207
Members
46,782
Latest member
ThomasGex

Latest Threads

Top