Skip to content

WebReflection/caller-of

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

19 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

caller-of

The tiniest yet most powerful JS utility ever :D

/** WTFPL Style License */
function callerOf(c) {return c.call.bind(c)}

What Does Above Code Do

Here is what we usually do

object.hasOwnProperty(key)

Here is what callerOf create

hasOwnProperty(object, key)

so we can borrow any method at any time and reuse it in a minifier friendly way.

var bind = callerOf(Function.bind);

// easy log
var log = bind(console.log, console);
log('it works');

// common borrowed methods
var
  has = callerOf({}.hasOwnProperty),
  whoIs = callerOf({}.toString),
  forEach = callerOf([].forEach),
  slice = callerOf([].slice);

has({a:1}, "a"); // true
has({a:1}, "toString"); // false

whoIs([]);    // "[object Array]"
whoIs(false); // "[object Boolean]"

slice(document.querySelectorAll("*")); // array
(function (obj){
  var args = slice(arguments, 1);
}());

forEach(document.getElementsByTagName("body"), log);

Compatibility

Every JavaScript engine I know, included IE.

For node.js simply npm install caller-of then var callerOf = require('caller-of')

What If No Function.prototype.bind

You can use this tiny yet working polyfill ^_^

// 139 bytes gzipped
/*! (C) WebReflection, Mit Style License */
(function (P) {
  'use strict';
  if (!P.bind) {
    P.bind = function (s) {
      var
        c = this,
        l = [].slice,
        a = l.call(arguments, 1);
      return function bind() {
        return c.apply(s, a.concat(l.call(arguments)));
      };
    };
  }
}(Function.prototype));

About

The tiniest yet most powerful JS utility ever :D

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published