Pen Settings

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation


Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

              <h1>Composite Example</h1>
<p>There is an area where several properties (Components) are located including single buildings (Leaves) as well as building complexes (Composites). A building complex may include several buildings. The goal is to get the total number of dwellers living in the area regading area as a "super complex". Using Composite pattern we can handle complex objects (Composites) that contain other objects (Leaves and/or Composites) as if they were simple objects. Another common example involves military Unit (Component), Army (Composite) and ArmyUnit (Leaf).</p>
<button id="sender1">Add building</button>
<button id="sender2">Add complex</button>
<button id="reset">Reset</button>
<div class="items" id="properties">
<div class="items">
  Total dwellers: <span id="dwellersNum">0</span>
              body {padding:10px;}
#receiver {
  border:1px solid gray;
  padding: 5px;
div.items {
  border:1px solid #222; 
div.items div, div.items header {
  border-bottom:1px solid #000;
              // Component
function Property(dwellers) {
  this.dwellers = dwellers? dwellers : +prompt('Insert number of dwellers in the building:', 100);
// Leaf
function Building(dwellers) {, dwellers);
  this.type = 'Single building';
Building.prototype = Object.create(Property.prototype);
Building.prototype.getDwellers = function() {
  return this.dwellers;
// Composite
function Complex() {
  this.type = 'Building complex';
  this.buildings = [];
  this.propertyList = document.getElementById("properties");
Complex.prototype = Object.create(Property.prototype);
Complex.prototype.getDwellers = function() {
  var dwellers = 0;
  for(var i = 0; i < this.buildings.length; i++) {
    dwellers += this.buildings[i].getDwellers();
  return dwellers;
Complex.prototype.addProperty = function(property, inform) {
  // getting the current number of dwellers in the area. Area can be regarded as a Super Complex containing single buildings and building complexes.
  document.getElementById("dwellersNum").textContent = this.getDwellers();
  if(!inform) return;
  var record = document.createElement('div');
  record.textContent = property.type + " added: " + property.getDwellers() + ' dwellers';

function Setup() {} = function() {
  var area = new Complex(); // area that contains complexes and single buildings
  var btn1 = document.getElementById("sender1");
  btn1.addEventListener("click", function() {
  area.addProperty(new Building(), true); // adding a single building to the area
  var btn2 = document.getElementById("sender2");
  btn2.addEventListener("click", function() {
  var smallComplex = new Complex(); 
  // adding a building complex, made up of two buildings, to the area
  smallComplex.addProperty(new Building(), false);
  smallComplex.addProperty(new Building(), false);
  area.addProperty(smallComplex, true);
  var reset = document.getElementById("reset");
  reset.addEventListener("click", function() {
  var list = document.getElementById("properties");
  area.buildings.length = 0;
  document.getElementById("dwellersNum").textContent = '';
function removeMsg(elem) {
  var msgs =;
  msgs.forEach(function(element, index, array) {
   if(msgs[index].tagName != 'header'.toUpperCase()) {
var setup = new Setup();;
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.