mootools/mootools-core — Issue #2216: Class mutators order-dependent
Author: ibolmo
Created: 2012-01-19 08:02:06+00:00
State: open
Labels: enhancement, optimization, semantic

(from [Lighthouse](https://mootools.lighthouseapp.com/projects/2706/tickets/642))

Class::implement is looping through properties and evaluating class mutators as they are encountered now, which means that the mutations will have different results depending on order (i.e. putting Implements at the end will overwrite the Class methods). There are many side effects of this. Almost all JavaScript engines loop through object literals in order of definition, however that is not a part of the standard.

I propose moving the mutations before calling implement() in the Class function:

``` javascript
function Class(params){

if (params instanceof Function) params = {initialize: params};

var newClass = function(){
Object.reset(this);
if (newClass._prototyping) return this;
this._current = $empty;
var value = (this.initialize) ? this.initialize.apply(this, arguments) : this;
delete this._current; delete this.caller;
return value;
}.extend(this);

for (var mutator in Class.Mutators){
if (!params[mutator]) continue;
var value = Class.Mutators[mutator].call(params, params[mutator]);
if (value == null) delete params[mutator];
}

newClass.implement(params);

newClass.constructor = Class;
newClass.prototype.constructor = newClass;

return newClass;

};
```

This will make mutators work consistently independent of order (unless they literally conflict with each other) and give the mutators access to all the Class's params whereas before they only have access to whatever had been looped through already and attached to the prototype.