I want to stop using OOP in javascript and use delegation instead

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?

+4
source share
1 answer

1) How would I check if an object implements a specific behavior?

Most people do not bother testing for this method.

  • If you want to test methods to branch and do different things if they are detected or not, then you are probably doing something evil (this type of instanceof usually the smell of code in OO code)

  • If you just check if the object implements an interface for checking errors, then this is not much better than not testing and allowing to exclude if the method is not found. I don’t know anyone who usually does this check, but I’m sure someone out there does it ...

2) How to initialize an object without repeating the entire setup for composite objects?

If you complete the code to build an internal object in a function or class, I think you can avoid most of the repetition and connection.

3) Is there any loss of performance due to the fact that most of the functions launched in the application go through apply ()?

In my experience, I prefer to avoid talking to this if this is strictly necessary. this inconvenient, breaks inside callbacks (which I widely use for iteration and asynchronous use), and it's very easy to forget to set it correctly. I try to use more traditional approaches to composition. For instance:

  • The presence of each owned facility is completely independent, without the need to look at their brothers and sisters or the owner. This allows me to simply call its methods directly and allow it to be its own this .

  • Providing links to their objects to their owner in the form of a property or as a parameter passed to their methods. This allows composition blocks to contact the owner without depending on the correct set of this .

  • The use of mixins, the alignment of individual component units at the same level. It has big name clash problems, but allows everyone to see each other and share the same "this". Mixins also separates the code from changes in the composition structure, since different composite divisions will still be smoothed out with the same mixed object.

4) Any good resources?

I don’t know, so tell me if you find one :)

+1
source

Source: https://habr.com/ru/post/1393435/


All Articles