How to prevent Prototype to add custom methods

6 views
Skip to first unread message

Alex Hernandez

unread,
Aug 3, 2008, 10:25:16 PM8/3/08
to Prototype & script.aculo.us
Hi,

I'm using a Web application that has some OO JS classes with their own
methods and properties.

For some reason this library do some validation on some methods that
the objects owns. Due to the OO nature of Prototype (I guess) it adds
a lot of new methods to the existing objects (I've checked that using
Firebug DOM viewer), and some validations of the existing library
throw some errors regarding those method inherited from prototype.

My question is if I can avoid Prototype to add those methods to the
custom objects. Is there any workaround for this?

thanks in advance if anyone can help.


Alex

kangax

unread,
Aug 4, 2008, 2:26:11 AM8/4/08
to Prototype & script.aculo.us
On Aug 3, 10:25 pm, Alex Hernandez <alex.hernan...@gmail.com> wrote:
> Hi,
>
> I'm using a Web application that has some OO JS classes with their own
> methods and properties.
>
> For some reason this library do some validation on some methods that
> the objects owns. Due to the OO nature of Prototype (I guess) it adds
> a lot of new methods to the existing objects (I've checked that  using
> Firebug DOM viewer), and some validations of the existing library
> throw some errors regarding those method inherited from prototype.

Exactly which methods is it complaining about?

>
> My question is if I can avoid Prototype to add those methods to the
> custom objects. Is there any workaround for this?

Prototype doesn't really add anything to "custom" objects. It adds
methods to some of the native ones - Array.prototype,
String.prototype, etc. (but not Object.prototype). Any objects
inheriting from those extended objects share added methods.

It would be pretty hard to make prototype *not* extend them without
changing the code base significantly.

You could also take a look Protosafe http://www.protolific.net/

--
kangax

Alex Hernandez

unread,
Aug 4, 2008, 10:23:53 AM8/4/08
to Prototype & script.aculo.us
I can not tell 100% where the problem is, The library is an OLD
fashioned techniqued used by some IBM Lotus Web Application, and their
methods are not working when I just Include Prototype in my libraries.

I'll take a look at Protosafe and let you know.

Thanks for your response.

Alex

kangax

unread,
Aug 4, 2008, 11:35:41 AM8/4/08
to Prototype & script.aculo.us
On Aug 4, 10:23 am, Alex Hernandez <alex.hernan...@gmail.com> wrote:
> I can not tell 100% where the problem is, The library is an OLD
> fashioned techniqued used by some IBM Lotus Web Application, and their
> methods are not working when I just Include Prototype in my libraries.

This sounds like "for/in problem". The library probably enumerates
over arrays with for/in ( rather than using for(;;) ) which breaks due
to prototype's Array.prototype extensions.

--
kangax

Alex Hernandez

unread,
Aug 4, 2008, 3:12:15 PM8/4/08
to Prototype & script.aculo.us
Looks like, I think.

Is there any hack on this without 'touching' my existing library?

May be is my english, but I can't get entirely protosafe, but will be
over it later... (I'm in parallel with other projects :-S)

thanks again.

Alex

kangax

unread,
Aug 4, 2008, 4:10:02 PM8/4/08
to Prototype & script.aculo.us
On Aug 4, 3:12 pm, Alex Hernandez <alex.hernan...@gmail.com> wrote:
> Looks like, I think.
>
> Is there any hack on this without 'touching' my existing library?
>
> May be is my english, but I can't get entirely protosafe, but will be
> over it later... (I'm in parallel with other projects :-S)
>
> thanks again.

I'll ping John about this (the creator of Protosafe).

--
kangax

jdalton

unread,
Aug 4, 2008, 4:13:52 PM8/4/08
to Prototype & script.aculo.us
Hey Alex,

This evening I will post a version of Prototype that is compiled to
not extend Array natives
and instead will use $Array.

Cheer,
- JDD

Alex Hernandez

unread,
Aug 4, 2008, 4:33:07 PM8/4/08
to Prototype & script.aculo.us
hey, thanks a lot,

looking forward to get it ;0P


Alex

jdalton

unread,
Aug 5, 2008, 8:38:10 AM8/5/08
to Prototype & script.aculo.us
Here it is:
http://www.protolific.net/prototype.compat.js

Everything should work the same way except how you handle arrays:

Because the Array object is subclassed you no longer get the native
sugar from standard arrays. However, all arrays returned from
Prototype
methods should be pre-sweetened.

Use the following syntax for manually creating a sweetened array:
new $Array(10) //makes a new $Array with a length of 10
//OR
$Array('andrew','jd','juriy ','justin','mislav','sam','tobie');
//OR
$A(['a', 'b', 'c']);

Use new string $split() method to return an $Array:
'apples,oranges,bananas'.$split(',').each(function(fruit) { ... });

- JDD

jdalton

unread,
Aug 5, 2008, 8:45:26 AM8/5/08
to Prototype & script.aculo.us


Subclass is really the wrong term to use:
It returns a regular array thats been manually extended
(not from its prototype).

Alex Hernandez

unread,
Aug 5, 2008, 9:54:23 AM8/5/08
to Prototype & script.aculo.us
cool, I'll give it a try and let you know

thanks

Alex

Henry

unread,
Aug 5, 2008, 10:24:12 AM8/5/08
to Prototype & script.aculo.us
On Aug 5, 1:38 pm, jdalton wrote:
> Here it is:http://www.protolific.net/prototype.compat.js

In that code is there any reason why the - $Array - function could not
be reduced to:-

function $Array(){
return $Array.to(Array.apply(this, arguments));
}

- to produce precisely the same outcome?

jdalton

unread,
Aug 5, 2008, 12:16:03 PM8/5/08
to Prototype & script.aculo.us
Correct results:
console.log($Array(3)); // [3]
console.log(new $Array(3.3)); // [3.3]
console.log(new $Array(3)); // [undefined, undefined, undefined]

If I use:
return $Array.to(Array.apply(this, arguments));

console.log($Array(3)); // [undefined, undefined, undefined]
console.log(new $Array(3.3)); // RangeError: invalid array length
console.log(new $Array(3)); // [undefined, undefined, undefined]

Thanks for the suggestion, even though it fails,
I am always looking for ways to reduce and optimize.

- JDD

Henry

unread,
Aug 5, 2008, 1:25:58 PM8/5/08
to Prototype & script.aculo.us
As - Array(3.3) - and - new Array(3.3) - would also throw range errors
you will not have a consistent substitute for them in - $Array - if
you don't throw the same errors in the same places.

Still, if you insist:-

function $Array(ln){
return $Array.to(
(
(arguments.length == 1)&&
(typeof l == 'number')&&
((ln|0) != ln)
)?
[ln]:
Array.apply(this, arguments)
);
}

Henry

unread,
Aug 5, 2008, 1:28:24 PM8/5/08
to Prototype & script.aculo.us
On Aug 5, 6:25 pm, Henry wrote:
> On Aug 5, 5:16 pm, jdalton wrote:
<snip>
> function $Array(ln){
>   return $Array.to(
>     (
>       (arguments.length == 1)&&
>       (typeof l == 'number')&&
^^^
(typeof ln == 'number')&&

Alex Hernandez

unread,
Aug 5, 2008, 1:29:03 PM8/5/08
to Prototype & script.aculo.us
Henry, it seems that it works :oP

I'm doing some stress testing and let you know if is there any other
issue.

Thanks a lot for your help.


Alex

jdalton

unread,
Aug 5, 2008, 2:10:50 PM8/5/08
to Prototype & script.aculo.us
Henry,

You are correct about the errors being thrown for
new Array(3.3) and Array(3.3);

But I think there is some confusion.
$Array is not a parallel of Array:

$Array acts as [ ].
var foo = [3.3];
var foo = $Array(3.3); // -> [3.3]

var foo = [-1];
var foo = $Array(-1); // -> [-1]

where:
var foo = Array(-1) // errors
var foo = Array(3.3) //errors
var foo = Array(3) // [undefined, undefined, undefined]

new $Array acts as new Array() (except you are right $Array is
forgiving with respect to negative and floats):

var foo = new Array(3); // [undefined, undefined, undefined];
var foo = new $Array(3); // -> [undefined, undefined, undefined];

I might add the errors for the negative and floating values,
though I think I dig its tolerance of them.

- JDD

jdalton

unread,
Aug 5, 2008, 3:10:20 PM8/5/08
to Prototype & script.aculo.us
Ahh I just connected the dots "Henry" = Richard Cornford.

"Henry" -> rcor...@raindrop.co.uk ->
http://private.dnsstuff.com/tools/ipall.ch?ip=86.54.86.190

"Richard Cornford" -> ric...@litotes.demon.co.uk ->
http://private.dnsstuff.com/tools/ipall.ch?ip=212.229.126.254

Recent Cornford highlight:
http://groups.google.com/group/comp.lang.javascript/browse_thread/thread/e7145c0d89caf0a1/0e47478a1577d54d?lnk=gst#0e47478a1577d54d

I would have never expected to see you of all people on the Prototype
User mailing list.
Welcome to the neighborhood mate :)

- JDD

Alex Hernandez

unread,
Aug 7, 2008, 12:46:50 AM8/7/08
to Prototype & script.aculo.us
Guys,

after doing a lot of testing:

1) All the Array problems were fixed with the solution posted by
Henry. thanks mate.

2) Scriptaculuos has some problems when loading the differente modules
since they have some dependencies on some prototype Array extended
methods, so I humbly modified scriptaculous.js resulting compatible
with the modified version of prototype delivered here.

Here are the files

http://alexhernandez.cl/wp/wp-content/uploads/2008/08/prototypecompat.js
//Modified by Henry
http://alexhernandez.cl/wp/wp-content/uploads/2008/08/scriptaculouscompat.js
// Modified by me.


thanks again for all the responses.

Alex


On Aug 5, 3:10 pm, jdalton <John.David.Dal...@gmail.com> wrote:
> Ahh I just connected the dots "Henry" = Richard Cornford.
>
> "Henry" -> rcornf...@raindrop.co.uk ->http://private.dnsstuff.com/tools/ipall.ch?ip=86.54.86.190
>
> "Richard Cornford" -> rich...@litotes.demon.co.uk ->http://private.dnsstuff.com/tools/ipall.ch?ip=212.229.126.254
>
> Recent Cornford highlight:http://groups.google.com/group/comp.lang.javascript/browse_thread/thr...

jdalton

unread,
Aug 7, 2008, 8:46:24 AM8/7/08
to Prototype & script.aculo.us
As I mentioned above Richard's changes break the usage of $Array in
those edge cases.
It’s probably best to not use his modifications.

I have a script that auto modifies the files to use $Array and $split
ProtoSafe,
http://code.google.com/p/protosafe/, though it needs to be updated
(probably sometime this weekend)

Here is for the entire Scriptaculous package modified:
http://www.protolific.net/scriptaculous.1.8.1.compat.js

I am glad it worked for you :)

- JDD

jdalton

unread,
Aug 7, 2008, 8:52:11 AM8/7/08
to Prototype & script.aculo.us
Alex,

Just a quick heads up, I checked your script and

your file: http://alexhernandez.cl/wp/wp-content/uploads/2008/08/prototypecompat.js
doesn't contain Richard's modifications.

It is the exact same as the unmodified
http://www.protolific.net/prototype.compat.js

So there is no need for you revert anything :D

- JDD
Reply all
Reply to author
Forward
0 new messages