Skip to content

Class mutators order-dependent #2216

@ibolmo

Description

@ibolmo

(from Lighthouse)

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:

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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions