After long conversations with javascript, I gradually became convinced that OOP is not the way, at least not too long. Having two or three levels of inheritance is fine, but working with full OOP, like in Java, just seems to be inappropriate.
The language supports layout and delegation natively. I want to use just that. However, I am having problems reproducing certain benefits from OOP.
Namely:
- How to check if an object implements certain behavior? I thought of the following methods
- Check if the object has a specific method. But that will mean standardization of method names, and if the project is large, it can quickly become cumbersome and lead to the java problem (object.hasMethod ('emailRegexValidatorSimpleSuperLongNotConflictingMethodName') ... This will simply move the OOP problem and not fix it. Besides, I will not could find information on search performance if methods exist.
- Store each composite object in an array and check if the object contains a composer. Something like: object.hasComposite (compositorClass) ... But it is also not very elegant and OOP again, just not in a standard way.
- Each object has the property "implements" the array and leaves the responsibility to the object if it implements a certain behavior, whether using composition or initially. Flexible and simple, but requires memorization of a number of conventions. This is my preferred method so far, but I'm still looking.
- How to initialize an object without repeating the entire setup for composite objects? For example, if I have a class "textInput" that uses a certain number of validators that must be initialized by variables, and a class "emailInput" that uses the same validators, it is cumbersome to repeat the code. And if the validator interface changes, the code must change in every class that uses them. How easy is it for me to configure this? The API I think of should be as simple as doing object.compositors ('emailValidator', 'lengthValidator', '...')
- Is there any performance loss due to the fact that most of the functions launched in the application go through apply ()? Since I'm going to make extensive use of delegation, the main objects are likely to have almost no methods. All methods will be provided by linked objects.
- Any good resource? I read countless posts about OOP and delegation, as well as the benefits of delegation, etc., but I can't find anything that could discuss “javascript delegation correctly” within a large framework.
change
Additional explanations:
- I don't have the code yet, I'm working on a framework in a clean OOP, and I'm stuck and need multiple inheritance. Thus, I decided to completely abandon classes. Therefore, I am now simply at a theoretical level and trying to understand this.
- "Composition" may be the wrong word; I mean a compound template that is very useful for tree structures. It’s true that tree structures are rarely found at the front end (well, of course, for the DOM, of course), but I'm developing for node.js
What I mean by "switching from OOP" is that I am going to part with the definition of classes using the "new" operator, etc .; I intend to use anonymous objects and extend them using delegation. Example:
var a = {}; compositor.addDelegates(a,["validator", "accessManager", "databaseObject"]);
Thus, the "class" will be a function with predefined delegates:
function getInputObject(type, validator){ var input = {}; compositor.addDelegates(input,[compositor,renderable("input"+type),"ajaxed"]); if(validator){input.addDelegate(validator);} return input; }
It makes sense?
source share