I think this isn't a reasonable example to build a object oriented design. It's simple and should have a simple solution.
Anyway... putting each function into a super main "class" MainFun
has nothing to do with an "object oriented approach". It's the same design like before. If you want to start with OOP you should think about responsibilities and create objects for them. In this example we could extract a ColorPicker
from the MainFunc. And a RandomColorPicker
could have a dependency to a random engine which is just a function returning a random hex.
JSFiddle
var Main = function(colorPicker) {
var that = this;
this.colorPicker = colorPicker;
this.highlight = function(value) {
$(".conversationid").filter(function() {
return $(this).html() == value;
})
.css('background', 'transparent')
.parent()
.css('background', that.colorPicker.pickColor());
};
};
var RandomColorPicker = function(randomEngine) {
this.rand = randomEngine;
};
RandomColorPicker.prototype.pickColor = function() {
return '#' + this.rand() + "" + this.rand() + "" + this.rand();
};
var randHex = function() {
return (Math.floor(Math.random() * 106) + 150).toString(16);
};
var f = new Main(new RandomColorPicker(randHex));