patternjavascriptMinor
Should I differentiate object types by calling methods via variables?
Viewed 0 times
differentiatecallingmethodsshouldviavariablestypesobject
Problem
I have three JavaScript objects. The first one serves merely as a prototype. The other two are implementation of a specific type.
I am missing the option of a
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.
That's why I thought I could call a specific function dynamically.
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.
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.
Solution
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 :
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
}
})Code Snippets
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
}
})Context
StackExchange Code Review Q#47145, answer score: 4
Revisions (0)
No revisions yet.