cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

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.

            
              var pre = document.querySelector('pre');
function toString(object) {
  return Object.prototype.toString.call(object);
}
function stringify(object) {
  var type = typeof object;
  if (type == 'string') {
    return object;
  } else if (type == 'undefined') {
    return 'undefined';
  } else if (type == 'boolean') {
    return object ? 'true' : 'false';
  }
  return object.toString();
}
function log() {
  var str = [].map.call(arguments, stringify).join(' ');
  pre.innerHTML += str + '\n';
}
function separator() {
  log(new Array(40).join('-'));
}
function header(str) {
  if (pre.innerHTML) {
    log('');
  }
  separator();
  log(':: ' + str);
  separator();
  log('');
}
/* Regex */
function RepeatMatcher(chr, strict) {
  var re;
  if (strict) {
    re = new RegExp('^(?:' + chr + ')+$');
  } else {
    re = new RegExp('(?:' + chr + ')+');
  }
  /*
  this[Symbol.replace] = function(string, replacement) {
    return string.replace(re, replacement);
  };
  this[Symbol.match] = function(str) {
    return str.match(re);
  };
  this[Symbol.search] = function(string) {
    return string.search(re);
  };
  this[Symbol.split] = function(string) {
    return string.split(re);
  };
  */
  // same code repeated 3 times better to create Hight Order function
  
  function wrap(method) {
    return function(string) {
      var args = [].concat.apply([re], [].slice.call(arguments, 1));
      return string[method].apply(string, args);
    };
  }
  ['split', 'match', 'search', 'replace'].forEach((name) => this[Symbol[name]] = wrap(name));
} 
header('Regular Expressions');
var m = new RepeatMatcher('s', true);
log(JSON.stringify('ssss'.match(m)));
log(JSON.stringify('sass'.match(m)));
log(JSON.stringify('__www__'.replace(new RepeatMatcher('w'), (_) => _ + '_x')));
log(JSON.stringify('foo____bar___baz'.split(new RepeatMatcher('_'))));

/* Symbol.iterator */
function Todos(init) {
  var data = init != undefined ? (init instanceof Array ? init : [init] ) : [];
  this.append = function(item) {
    data.push(item);
  };
  this[Symbol.iterator] = function*() {
    for (var i=0; i<data.length; ++i) {
      yield data[i];
    }
  };
}
header('iterators');
var todos = new Todos(['one']);
todos.append('hey');
todos.append('Jo');
for (let todo of todos) {
  log(todo);
}

/* Symbol.species */
header('Symbol.species');

class Lorem extends Array {
    static get [Symbol.species]() {
        return this;
    }
}

class Ipsum extends Array {
    static get [Symbol.species]() {
        return Array;
    }
}
const noop = () => {};
log('instanceof Foo',
    new Lorem().map(noop) instanceof Lorem);
log('instanceof Bar',
       new Ipsum().map(noop) instanceof Ipsum);
log('instanceof Array',
       new Ipsum().map(noop) instanceof Array);

/* Symbol.toPrimitive */
header('Symbol.toPrimitive');

function Answer() {
  this[Symbol.toPrimitive] = function(hint) {
    log('convert to ' + hint);
    if (hint == 'string') {
      return 'the answer is 42';
    } else if (hint == 'number') {
      return 42;
    } else {
      return 42;
    }
  }
}

var answer = new Answer();
log(+answer);
log('' + answer);
log(String(answer));

/* Proxy */

function proxify() {
  var object = Object.create(arguments[0]);
  function convert(hint) {
    log('convert', hint);
    if (hint == 'number') {
      return object.foo;
    } else if (hint == 'string') {
      return JSON.stringify(object);
    }
  }
  var props = [];
  var proxy = new Proxy(function() {}, {
    get: function(target, name) {
      log('get', name);
      if (name == Symbol.toPrimitive) {
        return convert;
      }
      return object[name];
    },
    set: function(target, name, value) {
      log('set', name, value)
      props.push(name);
      object[name] = value;
    },
    construct: function() {
      log('construct');
      return proxify(object);
    },
    apply: function(target, thisArg, args) {
      return args.reduce(function(a, b) {
        return a + b;
      }, 0);
    },
    deleteProperty: function(target, name) {
      log('delete', name);
      if (props.indexOf(name) != -1) {
        delete object[name];
      }
    }
  });
  return proxy;
}

header('Proxy');
var Foo = proxify({foo: '10'});
log(Foo.foo);

var foo = new Foo();
log(foo.foo);
foo.bar = 20;
log(foo.bar);

delete foo.bar;
log(foo.bar);
delete foo.foo;
log(foo.foo);

log(foo(1,2,3));

log(+foo);
            
          
!
999px
🕑 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.
Loading ..................

Console