Week 5 | Class-style Constructor

polymer 1.0
Mon Sep 26 2016

With the class-style constructor we can set up our element’s prototype chain but not register it immediately. So instead of writing

<hello-element></hello-element>

Polymer({is:'hello-element',
      created: function(){
      this.textContent = 'Hello there'}
})

we can first declare our element’s blueprint

        HelloElement = Polymer.Class({
          is: "hello-element",
          created: function() {
              this.textContent = "Hello there";
          }
        });

then register our element

document.registerElement('hello-element', HelloElement);

and lastly create an instance whenever needed

        var el = new HelloElement();
        document.querySelector("body").appendChild(el);

Full Stack Weekly Newsletter

A free weekly newsletter for full stack web developers!