E
E
EvelynWaugh2021-06-16 17:45:05
JavaScript
EvelynWaugh, 2021-06-16 17:45:05

What is the name of this kind of class in js?

Here is the class, and there is a return inside, I don't see this in the MDN documentation, but it somehow works.

const TestUtil = function() {
    const testHandlers = [];

        const  _testHandlerInit = () => {
          console.log('works')
        }

    return {                       /////// С вот таким return

        init: function(settings) {
            _testHandlerInit ();
        },
      
      avrukh: function() {
        console.log('avg')
      }

    }
}
let util = new TestUtil();

util.init()  /// 'works'
util._testHandlerInit()   ///    Ошибка. Может return показывает к каким методам есть доступ а остальные вспомогальные?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Aetae, 2021-06-16
@EvelynWaugh

Well, initially in js, a class is just a function that returns an object. By default, this is the new this, but you can return anything to confuse the enemy.)
No name, just a controversial feature of the language.

S
SUS_ITS_LOVE, 2021-06-16
@SUS_ITS_LOVE

I'm not sure, but it seems that you mean a class with a constructor

D
DimaIs, 2021-06-16
@DimaIs

You return a new object with the following signature:

{      
        init: function,
        avrukh: function
}

When init is called , it has access to the internal context, which contains testHandlers . If you want to call this function on the returned object, you can add this method to the field of the returned object:
{      
        testHandlers: testHandlers,
        init: function,
        avrukh: function
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question