I am new to javascript and my question might be outrightly stupid. I am not able to get the correct reference for this.
I am trying to write a controller object in javascript. The idea here is to make this an interface object where anyone can inherit from its prototype and override the methods for their being.
I just want to expose one method "process()" to the object and this method will call all the remaining methods and attributes. I am trying to achieve this by making all the other methods and attributes private by using closures. this is the sample snippet of my code
var ViewController = function() {
var _viewName = null;
var loadView = function(viewName) {
console.log("now loading view : "+viewName+ " ...");
};
return {
process : function(viewName){
_viewName = viewName;
loadView(_viewName);
}
};
};
var vController = ViewController();
vController.process("alphaView");
Now this works perfectly fine. But if I want to override the methods PreProcess, loadView, initView and PostProcess using prototype, it doesn work. I tried overriding using the Object.create like
var vController2 = Object.create(ViewController, {
LoadView : function(viewName) {
//some custom implementation
},
//and so forth
});
And if I change my implementation to define the methods outside using prototypes also doesn't work. Like
var ViewController = function() {
var _viewName = null;
return {
process : function(viewName){
_viewName = viewName;
loadView(_viewName);
}
};
};
ViewController.prototype.loadView = function(viewName) {
console.log("now loading view : "+viewName+ " ...");
};
The gist of my issue being that I want (a) an interface object for a controller where the user can override the the one basic method (LoadView,) (b) All these members are private to the controller object and be accessible via the "Process()" method
Please advise!
EDIT: edited the code to make it simple.