Skip to content

Instantly share code, notes, and snippets.

@WebReflection
Last active December 23, 2015 11:49
Show Gist options
  • Select an option

  • Save WebReflection/6631470 to your computer and use it in GitHub Desktop.

Select an option

Save WebReflection/6631470 to your computer and use it in GitHub Desktop.
utils
var utils = (function (Object) {
'use strict';
var
ObjectPrototype = Object.prototype,
ArrayPrototype = Array.prototype,
call = Object.call,
utils = {};
function bind(name) {
if (typeof this[name] === 'function') {
utils[name] = call.bind(call, this[name]);
}
}
utils.query = function query(CSS, parentNode) {
return utils.slice(
(parentNode || document).querySelectorAll(CSS)
);
};
Object
.getOwnPropertyNames(ArrayPrototype)
.forEach(bind, ArrayPrototype);
Object
.getOwnPropertyNames(ObjectPrototype)
.forEach(bind, ObjectPrototype);
return utils;
}(Object));
@WebReflection

Copy link
Copy Markdown
Author

some example …

utils.forEach([1,2,3], alert);

// extendible with something silly like this
// when or if needed
utils.addEventListener = function (el) {
  el.addEventListener(this.name, this);
};
utils.removeEventListener = function (el) {
  el.removeEventListener(this.name, this);
};

utils.query('*').forEach(
  utils.addEventListener,
  function click(e){
    alert(e.type);
  }
);

@WebReflection

Copy link
Copy Markdown
Author

more examples …

utils.hasOwnProperty(object, key);

utils.toString([]); // [object Array]

and similar stuff ..

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment