Jump to content

Best method to keep users from overwriting methods but giving them the ability to adjust them?


MrFish

Recommended Posts

Here is my situation. I have a javascript class I'm calling PanelOS (managing panels and such in an application). I want it to log errors, warnings, and messages. I have three methods to do this: logError, logWarning, logMessage. When these are used I want it to create a Log object and push it to these arrays and also call an onerror, onwarning, and onmessage event. I want all of theses events to call onlog as well. With this I will have any kind of view that is using it (lets say a black screen with white text for starters) be able to use the onerror event without overwriting the onerror method from calling onlog. This is my first time using prototype so if it looks funny that's why.

function PanelOS(){}PanelOS.prototype.errors = null;PanelOS.prototype.warnings = null;PanelOS.prototype.messages = null;PanelOS.prototype.constructor = function(){}PanelOS.prototype.loadScript = function(src){var head = document.getElementById("head");if(head != null){}else{  this.logError("document head does not exist");}}PanelOS.prototype.logError(str){var Lg = new Log();Lg.type = "error";Lg.message = str;this.onerror(Lg);this.errors.push(Lg);}PanelOS.prototype.logWarning(str){var Lg = new Log();Lg.type = "error";Lg.message = str;this.onwarning(Lg);this.warnings.push(str);}PanelOS.prototype.logMessage(str){var Lg = new Log();Lg.type = "message";Lg.message = str;this.onmessage(lg);this.messages.push(str);}// EventsPanelOS.prototype.onerror = function(Lg){this.onlog(Lg);}PanelOS.prototype.onwarning = function(Lg){this.onlog(Lg);}PanelOS.prototype.onmessage = function(Lg){this.onlog(Lg);}PanelOS.prototype.onlog = function(Lg){}  // Helper Objects function Log(){this.type = "Undefined";this.message = "Undefined";}

So is there a way I can use the method in many 3rd party scripts without overwriting each other or the original? I could make something like an addEventListener to this but I'm not sure how I would do that.

Link to comment
Share on other sites

Ok I think I found the solution myself. PanelOS extends Object which handles events.

function Object(){}Object.eventListeners = null;Object.prototype.constructor = function(){this.eventListeners = [];}Object.prototype.dispatchEvent = function(evtName){for(var i = 0; i < this.eventListeners.length; i++){  var Evt = this.eventListeners[i];  if(Evt.name == evtName)   Evt.callback();}}Object.prototype.addEventListener(nm, callback){var Evt = new Event();Evt.name = nm;Evt.callback = callback;this.eventListeners.push(Evt);return Evt;}function Event(){this.name = "Undefined";this.callback = function(){};}

PanelOS dispatches events. The events with that name run the callback.

/** #include <object.js>****/function PanelOS(){}PanelOS.prototype = new Object();PanelOS.prototype.BootLoader = null;PanelOS.prototype.ScriptLoader = null;PanelOS.prototype.StyleLoader = null;PanelOS.prototype.errors = null;PanelOS.prototype.warnings = null;PanelOS.prototype.messages = null;PanelOS.prototype.logs = null;PanelOS.prototype.constructor = function(){}PanelOS.prototype.loadScript = function(src){var head = document.getElementById("head");if(head != null){}else{  this.logError}}PanelOS.prototype.logError(str){var Lg = new Log();Lg.type = "error";Lg.message = str;this.errors.push(Lg);this.logs.push(Lg);this.dispatchEvent("error");}PanelOS.prototype.logWarning(str){var Lg = new Log();Lg.type = "warning";Lg.message = str;this.warnings.push(str);this.logs.push(Lg);this.dispatchEvent("warning");}PanelOS.prototype.logMessage(str){var Lg = new Log();Lg.type = "message";Lg.message = str;this.messages.push(str);this.logs.push(Lg);this.dispatchEvent("message");}

Is that how everyone else would have done it?

Link to comment
Share on other sites

If you're really trying to keep people/users from overwriting anything, I wouldn't advocate for the use of this. It is essentially the same thing as making public member/method in Java. If you intended to develop this as an API, I would look into some of the design patterns covered in these books.http://www.amazon.co...design+patternshttp://www.amazon.co...design+patterns Since Javascript has no support for things in Java like final or private/protected, I have come to represent this kind of variables in JS using var within the context of a function/class, and then using closures to make these variable available in the functions I do expose (either by returning an object of funtions, or using this on th) while still keeping them shielded from the user. Not to say that there is anything wrong with using prototype, just be aware of what is actually publicly available to your users or not.

Edited by thescientist
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...