0

I am trying to extend a Leaflet class, and looking at the documentation it gives this example:

var MyBoxClass = L.Class.extend({

    options: {
        width: 1,
        height: 1
    },

    initialize: function(name, options) {
        this.name = name;
        L.setOptions(this, options);
    }
    
});

var instance = new MyBoxClass('Red', {width: 10});

console.log(instance.name); // Outputs "Red"
console.log(instance.options.width); // Outputs "10"
console.log(instance.options.height); // Outputs "1", the default

However, if I try to access this.options.height in initialize after calling L.setOptions it is undefined. Instead there appears to be a this.options.prototype.height.

How should I be accessing options members so I get the default if the caller doesn't define a value?

Thanks

4
  • I may be misunderstanding your question but this.options.height in this example jsfiddle.net/hb6e5mo2 seems to work just fine
    – nikoshr
    Commented Jun 18 at 10:14
  • I'm running my code in chrome and inside the initialize function after the call to SetOptions my options member shows as undefined
    – Jules
    Commented Jun 18 at 14:05
  • Had trouble looking at the fiddle while mobile. Now looked at it and it's doing exactly what I expected (ie working), so it must be something about my version / environment. I'll try to replicate it in fiddle.
    – Jules
    Commented Jun 18 at 16:47
  • I figured it out, it turns out I had a bug that was explicitly setting the member I thought I couldn't access to undefined much earlier. Thank-you for your help
    – Jules
    Commented Jun 18 at 19:12

0

Browse other questions tagged or ask your own question.