What does MooTools' Function.prototype.overloadSetter() do?

68 views
Skip to first unread message

HENG

unread,
Oct 24, 2010, 9:15:25 AM10/24/10
to mootools-users

Hello guys:

I am very confused about Mootools 1.3 source code and especailly the code as follows:

  1.     var enumerables = true;
  2.     for (var i in {
  3.         toString: 1
  4.     }) enumerables = null;
  5.     if (enumerables) enumerables = ['hasOwnProperty', 'valueOf', 'isPrototypeOf', 'propertyIsEnumerable', 'toLocaleString', 'toString', 'constructor'];
  6.     Function.prototype.overloadSetter = function(usePlural) {
  7.         var self = this;
  8.         return function(a, b) {
  9.             if (a == null) return this;
  10.             if (usePlural || typeof a != 'string') {
  11.                 for (var k in a) self.call(this, k, a[k]);
  12.                 if (enumerables) for (var i = enumerables.length; i--;) {
  13.                     k = enumerables[i];
  14.                     if (a.hasOwnProperty(k)) self.call(this, k, a[k]);
  15.                 }
  16.             } else {
  17.                 self.call(this, a, b);
  18.             }
  19.             return this;
  20.         };
  21.     };
  22.     Function.prototype.overloadGetter = function(usePlural) {
  23.         var self = this;
  24.         return function(a) {
  25.             var args, result;
  26.             if (usePlural || typeof a != 'string') args = a;
  27.             else if (arguments.length > 1) args = arguments;
  28.             if (args) {
  29.                 result = {};
  30.                 for (var i = 0; i < args.length; i++) result[args[i]] = self.call(this, args[i]);
  31.             } else {
  32.                 result = self.call(this, a);
  33.             }
  34.             return result;
  35.         };
  36.     };
'Function.property,overloadSetter' and 'Function.property.overloadGetter' .... I found they really important function in Mootools 1.3, but no document and no explaintion about it........How does it works with Mootools core? Who can give me an explaintion?

And more, I really have no idea about 'var enumerables = true;'  why do we need enumerable? I know it has some different in ECMA-262 Javascript, But I really do not know why Mootools use it....

Anyone can understand the code....I will admire you so much, because I do not understand it....

Thank you....


--
--------------------------------------------------------------------
HengZhou
---------------------------------------------------------------------
--

Fábio M. Costa

unread,
Oct 24, 2010, 11:07:19 AM10/24/10
to mootool...@googlegroups.com
These 2 functions can be used by you, but as they are not documented, they might change over time, we don't recommend that you use it. Use it at your own risk.

The overloadSetter grabs a normal function that accepts 2 arguments, a key and a value and turns it into a function that can accept an object as the parameters too.

so, for example:

Function.prototype.extend = function(key, value){
    this[key] = value;
};

This will make the extend function accept a key and a value that will be attached to "this" function. Like this:

function(){}.extend('key', 'value');

When you do:

Function.prototype.extend = function(key, value){
    this[key] = value;
}.overloadSetter();

The function will be able to accept an object too and it will be possible to use it like this:

function(){}.extend({'key': 'value'});


The overloadGetter function will transform a function that receives one string and returns one value, like the get function:

get: function(prop){
        var property = Element.Properties[prop];
        return (property && property.get) ? property.get.apply(this) : this.getProperty(prop);
}.overloadGetter()

Into a function that can accept an array or multiple arguments, so you can use it this:

get('p1', 'p2', 'p3', ...) or get(['p1', 'p2', 'p3', ...])


Hope it's clear.
Again, avoid using it for public projects.

--
Fábio Miranda Costa
frontend@portalpadroes
Globo.com
github: fabiomcosta
twitter: @fabiomiranda
ramal: 6410

Tim Wienk

unread,
Oct 24, 2010, 11:51:07 AM10/24/10
to mootool...@googlegroups.com
2010/10/24 Fábio M. Costa <fabio...@corp.globo.com>:

> These 2 functions can be used by you, but as they are not documented, they
> might change over time, we don't recommend that you use it. Use it at your
> own risk.

To be more clear: overloadSetter and overloadGetter are private API.
They can change without notice, or even be removed without notice,
since those are considered internal changes. Anything not documented
is part of the private API.

So as Fábio says: usage is possible, but at your own risk.

אריה גלזר

unread,
Oct 24, 2010, 12:09:17 PM10/24/10
to mootool...@googlegroups.com


On Sun, Oct 24, 2010 at 5:51 PM, Tim Wienk <timw...@gmail.com> wrote:
So as Fábio says: usage is possible, but at your own risk.

I think the reason HENG asked was to understand the code, not to use it (I know I myself spent quite a lot of time reading the 1.2 code learning from it. Still haven't managed to find the time to read 1.3 though...)

very cool functionality!

--
Arieh Glazer
אריה גלזר
052-5348-561
5561

HENG

unread,
Oct 24, 2010, 9:48:26 PM10/24/10
to mootool...@googlegroups.com
Thank Fábio M. Costa's Explation....Thank you... if you do not give me some tips and I can not understand the code by now....

I know it is private API, but what I mean is how to understand HOW Mootools to work. Most of us really do NOT care about how the framework to work, but not for me. I can understand jQuery's code very well, so I even can write some code as jQuery, and I know how the Chain work.

To me, know how to use Mootools is not enough. Especially to the new to javascript, as me. Mootools is very good framework, its code always can let think how to make the code be more prefect. So, I need to know how the frmework works. And more, Mootools' code is really beautiful, and even I am a new, I want to give you all a suggestion: reading the source code of Mootools is the best guid to advance javascript.

Thank you all guys....  Your answers are really important to me ....  The mail list can help a lot of people that love Mootools.... Thank you.....

Thank you very much....




2010/10/25 אריה גלזר <arieh....@gmail.com>

Fábio M. Costa

unread,
Oct 24, 2010, 10:15:54 PM10/24/10
to mootool...@googlegroups.com
Oh, thank YOU, for you passionate reply! :D
Thanks for checking out the code.


--
Fábio Miranda Costa
frontend@portalpadroes
Globo.com
github: fabiomcosta
twitter: @fabiomiranda
ramal: 6410



Dimitar Christoff

unread,
Oct 25, 2010, 7:22:13 AM10/25/10
to mootool...@googlegroups.com
FYI and in case anyone else wants to know, Keeto just answered this on
stackoverflow (mwahahah)

http://stackoverflow.com/questions/4006516/what-does-mootools-function-prototype-overloadsetter-do

Best regards,
--
Dimitar Christoff <chri...@gmail.com>

blog: http://fragged.org/ twitter: @D_mitar

Reply all
Reply to author
Forward
0 new messages