This project is archived and is in readonly mode.

#982 ✓invalid
giorgiga

Options and instanceof

Reported by giorgiga | September 5th, 2010 @ 10:01 PM

The Options mix-in, and something in the Class constructor, seem to alter the nature of option values (do they deep-copy the attributes?)

Here is a test case illustrating the issue
(it makes use of console.debug)


// some non-mootools object (could have been google.maps.LatLng)
var O = function(id) { this.id = id };

// class defition 1: implements Options / prototype.options set through new Class()
var C1 = new Class({
    Implements: Options,
    options: { p: new O('default value') },
    initialize: function(options) { this.setOptions(options); }
});

// class defition 2: implements Options / prototype.options set manually
var C2 = new Class({
    Implements: Options,
    initialize: function(options) { this.setOptions(options); }
})
C2.prototype.options = { p: new O('default value') };

// class defition 3: does not implements Options / prototype.options set through new Class()
var C3 = new Class({
    options: { p: new O('default value') },
    initialize: function(options) { this.options = $H(options).combine(this.options); }
})

// class defition 4: does not implements Options / prototype.options set manually
var C4 = new Class({
    initialize: function(options) { this.options = $H(options).combine(this.options); }
})
C4.prototype.options = { p: new O('default value') };

// test1
// result: FAIL 
//         PASS -- prototype.options set manually
//         FAIL
//         PASS -- prototype.options set manually
[C1,C2,C3,C4].each(function(c,i){
    console.debug('C' + (i+1), ' -> ', 
                  c.prototype.options.p instanceof O ? 'PASS' : 'FAIL');
});

var cs = {
    c1a: new C1(), c1b: new C1({p:new O('custom  value')}),
    c2a: new C2(), c2b: new C2({p:new O('custom  value')}),
    c3a: new C3(), c3b: new C3({p:new O('custom  value')}),
    c4a: new C4(), c4b: new C4({p:new O('custom  value')})
}

// test 2
// result: FAIL, FAIL -- implements Options
//         FAIL, FAIL -- implements Options
//         FAIL, PASS -- succedes iff option is overridden (ie: not from prototype.options)
//         PASS, PASS
$H(cs).each(function(v,k,h){
    console.debug(k, ' -> ', cs[k].options.p.id, ' ',
                  cs[k].options.p instanceof O ? 'PASS' : 'FAIL');
})

Comments and changes to this ticket

  • Christoph Pojer

    Christoph Pojer September 6th, 2010 @ 11:22 AM

    • State changed from “new” to “invalid”

    MooTools unlinks (copies) all data in options. If you need to pass an instance of one of your classes you should do it outside of options (as a normal argument).

  • Sebastian Markbåge

    Sebastian Markbåge September 6th, 2010 @ 03:02 PM

    MooTools also deep-copies objects that are attached to any class property. You can't currently pass an instance through the Class definition. Unless you give access to it through a variable from your Classes functions. Perhaps wrapping it all in a closure. You can also attach it to a property on the class constructor itself.

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

Shared Ticket Bins

Pages