4
\$\begingroup\$

I have three JavaScript objects. The first one serves merely as a prototype. The other two are implementation of a specific type.

var MainPrototype = {};
var SpecificType = $.extend(Object.create(MainPrototype));
var OtherSpecificType = $.extend(Object.create(MainPrototype)); 

I am missing the option of a parent-method and am trying to implement some specifics for each specific type.

I started of by comparing the types within the main prototype object, yet I found the nesting if-else clausing worrysome and in case that new types would be introduced, I guess the code will get ugly fast. I also does not feel right to put the specifics into the main function.

var MainPrototype = {
    function: bind() {
        //generic stuff

        if (this.type === SpecificType.type) {
          // specifics for this type
        } else if (this.type === OtherSpecificType.type) {
          // specifics for other type
        }
    }
}

That's why I thought I could call a specific function dynamically.

var MainPrototype = {
    bind: function() {
        //generic stuff
        var specificBindMethod = this.type + "Bind";
        if (typeof(this[bindMethod]) === "function") {
            this[categoryBindMethod]();
        }
    }
};

var SpecificType = $.extend(Object.create(MainPrototype, {
  type: 'specific',
  specificBind: function() {
    // doSpecifics
  }
});
var OtherSpecificType = $.extend(Object.create(MainPrototype, {
  function: 'other',
  otherBind: function() {
    // doSpecifics
  }
}) 

Now I can define the specific function within its proper object and only if they exist they will get called getting rid of the nesting.

I am wondering if this approach is better in terms of maintainability and extensibility or if I should have stayed with the if-else-approach or if there is anything better to solve my current use case.

\$\endgroup\$
6
  • 2
    \$\begingroup\$ What is the goal of this code? \$\endgroup\$
    – Joseph
    Apr 14, 2014 at 14:39
  • 1
    \$\begingroup\$ Any reason to not simply overwrite bind in the subclasses? You, as you say, implement specifics for the specific classes. Sometimes that means overriding the parents class' function \$\endgroup\$
    – Flambino
    Apr 14, 2014 at 15:23
  • \$\begingroup\$ @Flambino That is what I mean by stating that I miss the parent method. I do not want to duplicate the generic parts, but keep that in the main object. If I would do this in another language I would do something on the lines of SpecificObject.bind{ parent.bind(); doSpecificStuff() }; \$\endgroup\$
    – k0pernikus
    Apr 14, 2014 at 15:31
  • 1
    \$\begingroup\$ @k0pernikus Ah, ok, now I understand your question. But yes, you can use the code in the SO answer. Or, depending on how you set up your inheritance mechanism, you can have the parent available, or have super available in all methods (CoffeeScript does this) \$\endgroup\$
    – Flambino
    Apr 14, 2014 at 16:37
  • 1
    \$\begingroup\$ I'm not entirely sure this applies, but John Resig's Simple Class Inheritance provides super to access overridden methods. \$\endgroup\$ Apr 15, 2014 at 2:02

1 Answer 1

4
\$\begingroup\$

Why wouldn't you create a method with a unique name accross all subclasses? In this way you don't have to generically build a function name, which can be a pain to maintain code / refactor

For instance :

var MainPrototype = {
    bind: function() {
        //generic stuff
        if (typeof(this.subBind) === "function") {
            this.subBind();
        }
    }
};

var SpecificType = $.extend(Object.create(MainPrototype, {
    type: 'specific',
    subBind: function() {
        // doSpecifics
    }
});
var OtherSpecificType = $.extend(Object.create(MainPrototype, {
    function: 'other',
    subBind: function() {
        // doSpecifics
    }
})
\$\endgroup\$
1
  • \$\begingroup\$ No reason other than it stems from my if-else-mess :) \$\endgroup\$
    – k0pernikus
    Apr 14, 2014 at 15:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.