Categories


Archives


Recent Posts


Categories


New Keyword Not Required for UI Component Javascript Objects

astorm

Frustrated by Magento? Then you’ll love Commerce Bug, the must have debugging extension for anyone using Magento. Whether you’re just starting out or you’re a seasoned pro, Commerce Bug will save you and your team hours everyday. Grab a copy and start working with Magento instead of against it.

Updated for Magento 2! No Frills Magento Layout is the only Magento front end book you'll ever need. Get your copy today!

In doing research for the new uElement Internals series, I discovered that Magento’s UI Component object constructors (uiClass, uiElement, uiCollection, etc) can create new objects without the use of the new keyword.

//both return a new `UiElement` object
object = new UiElement;
object = UiElement();

This makes these constructor objects closer in behavior to Magento’s base helper classes like Object and Array. It is curious, however, that the view model registration code

#File: vendor/magento//magento2-base/lib/web/mage/layout.js
function initComponent(node, Constr) {    
    var component = new Constr(_.omit(node, 'children'));

    registry.set(node.name, component);
}

choses to use the new keyword when instantiating these objects. Perhaps it’s a sign that there’s multiple developers not talking to one another, or that there’s weird bugs/differences when using or not using the new keyword. One is, once again, left in the dark without decent documentation on the subject.

Copyright © Alan Storm 1975 – 2019 All Rights Reserved

Originally Posted: 5th December 2016