2

The underscore javascript library contains a method named extend. You can use extend to copy properties of one javascript object to another javascript object in a consistent way. These properties can be data, or javascript functions.

Magento 2 uses this to give objects methods. You can see any example of this here

#File: vendor/magento/module-ui/view/base/web/js/core/renderer/layout.js
_.extend(layout, {
    iterator: function (parent, node) {
    //...
});

By copying the interator property to the layout object, the underscore JS library gives the layout object an interator method.

What I don't understand is why Magento ends calling extend multiple times. From the same file.

_.extend(layout, {
    iterator: function (parent, node) {
    //...
});

_.extend(layout, {
    waitTemplate: function (parent, node) {
    //...
});

_.extend(layout, {
    manipulate: function (node) {
    //...
});

I get that each of these extend calls copies a different set of properties -- but is there a compelling technical reason Magento's core developers have spread these out into three different calls? Or is this just style (possibly laziness/busyness) showing its hand?

1 Answer 1

1

After a lot of research, this appears to be nothing more than the team's personal style/quirks. The three different calls to extend seem to group methods by functionality

Main App Methods:       iterator, process, build, initComponent
Async Methods:          waitTemplate, waitParent, applyTemplate
Data Get/Set Methods:   manipulate, insert, insertTo, addChild, merge,
                        getDataSources, getFullConfig, getByProperty,
                        filterComponents

However, there's nothing in this separation that effects the actual behavior of the layout object. Its just a team choice as to how things are organized.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.