Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/WebReflection/caller-of
The tiniest yet most powerful JS utility ever :D
https://github.com/WebReflection/caller-of
Last synced: 1 day ago
JSON representation
The tiniest yet most powerful JS utility ever :D
- Host: GitHub
- URL: https://github.com/WebReflection/caller-of
- Owner: WebReflection
- License: wtfpl
- Created: 2013-02-15T21:18:33.000Z (over 11 years ago)
- Default Branch: master
- Last Pushed: 2015-04-01T23:54:41.000Z (over 9 years ago)
- Last Synced: 2024-09-21T12:27:58.865Z (about 2 months ago)
- Language: HTML
- Homepage:
- Size: 363 KB
- Stars: 104
- Watchers: 4
- Forks: 4
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
README
caller-of
=========The tiniest yet most powerful JS utility ever :D
```JavaScript
/** WTFPL Style License */
function callerOf(c) {return c.call.bind(c)}
```### What Does Above Code Do
Here is what we usually do
```JavaScript
object.hasOwnProperty(key)
```
Here is what `callerOf` create
```JavaScript
hasOwnProperty(object, key)
```
so we can borrow any method at any time and reuse it in a minifier friendly way.```JavaScript
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"); // falsewhoIs([]); // "[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 ^_^
```JavaScript
// 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));
```