Checking if parent class has method defined


#1

What would be a correct way to check if a method is overriding a method from parent class? For example:

Ember.Object.createWithMixins({
    foo: function() {
        if (/*Object has foo defined, and our implementation is overriding it) this._super();
        else /* Let's do something else here instead */
    }
})

#2

Maybe something like this?

typeof(this._super()) == “function”


#3

If that doesn’t work, then I would try:

Ember.Object.createWithMixins({
    foo: function() {
        if (this._super) this._super();
        else /* Let's do something else here instead */
    }
})

#4

this._super is always going to be a function. Here’s the code:

this._super = superFunc || K;

So you’d have to compare against Em.K if you wanted to know whether the super function existed or not. Personally, I would use a closure and an instantiated instance of the parent class.

Em.Object.create({
    foo: (function(overrides) {
        return function() {
            if (overrides) {} else {}
        };
    })(!!Em.Object.create().foo)
});

#5

Thanks for the replies guys. Seems like checking for this.__nextSuper does what I like to do. E.g.

Em.Object.createWithMixins({
    foo: function() {
        if (this.__nextSuper) this._super()
        else /* do something else instead */
    }
});

#6

It should be noted that __nextSuper is only defined in the 1.5.0 beta, not in 1.4.0.