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 CSS

These stylesheets will be added in this order and before the code you write in the CSS editor. You can also add another Pen here, and it will pull the CSS from it. Try typing "font" or "ribbon" below.

Quick-add: + add another resource

Add External JavaScript

These scripts will run in this order and before the code in the JavaScript editor. You can also link to another Pen here, and it will run the JavaScript from it. Also try typing the name of any popular library.

Quick-add: + add another resource

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
Loading ..................

Console