473,414 Members | 1,723 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,414 software developers and data experts.

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

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
3 3554
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Martin Magnusson | last post by:
Hi! I have a class with a private member which is a pointer to an abstract class, which looks something like this: class Agent { public: void Step( Base* newB ); private:
7
by: Santi | last post by:
I have two classes: Product and Fruit which inherits from Product. If I try to narrow the reference to the base type by a cast, I always get a reference to the inherited type. For example: ...
4
by: alee.indy | last post by:
Basically, I want to add default methods to the PHP string object so that I can do something like: $stringvar = "hello"; $stringvar -> append(" world"); // where append is some kind of...
4
by: FacultasNetDeveloper | last post by:
I am extending FileSystemInfo class so that I can Implement Icomparable. My custom class looks like: Class FSFileSystemInfo Inherits FileSystemInfo : Implements Icomparable Function...
4
by: Jeff | last post by:
The derived class below passes a reference to an object in its own class to its base calss constructor. The code compiles and will run successfully as long as the base class constructor does not...
3
by: iogilvy | last post by:
i wish to have some extended functionality added to sockets i can create my own socket class class mysocket(socket.socket): and all should be fine. Except, the sockets are created for me by...
7
by: Jessica | last post by:
Hi, I have a question referring prototypal inheritance in javascript. My example: function A() {}; A.prototype = { count: 10, doit : function () {alert ("doit");} };
1
by: subramanian100in | last post by:
Consider class Base { .... }; class Derived : public Base { ...
1
by: Wiinie | last post by:
print Dumper($englishTB); ==> this is a result $VAR1 = bless( { 'TEXT' => 'english- This is a text ', 'LANGUAGE' => bless( { ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.