javascript - Compressing Function.prototype.bind into 140 characters -
this bind polyfill function mdn:
function.prototype.bind = function(othis) { if (typeof !== 'function') { // closest thing possible ecmascript 5 // internal iscallable function throw new typeerror('function.prototype.bind - trying bound not callable'); } var aargs = array.prototype.slice.call(arguments, 1), ftobind = this, fnop = function() {}, fbound = function() { return ftobind.apply(this instanceof fnop ? : othis, aargs.concat(array.prototype.slice.call(arguments))); }; if (this.prototype) { // function.prototype doesn't have prototype property fnop.prototype = this.prototype; } fbound.prototype = new fnop(); return fbound; };
i wanted compress 140 characters or less using es2015 syntax.
does following achieve same goal (albeit not method on function.prototype
)?
var bind=(f,t,...a)=>{ function g(...b){ return f.call(this instanceof g?this:t,...a,...b) } g.prototype=object.create(f.prototype||{}); return g }
i particularly interested in whether instanceof
check equivalent.
edit: g
converted non-arrow function (which breaks character limit 1 character if whitespace removed)
your es6 syntax nicer
binding shim condensed 161
var bound1 = myfunction.bind(arg1, arg2, arg3); function.prototype.bind=function(d){ var s=array.prototype.splice, a=s.call(arguments,1), c=this; return function(){ return c.apply(d,a.concat(s.call(arguments,0))) } }
binding utility function 103
var bound2 = bind(myfunction, [arg1, arg2, arg3]); function bind(f,a) { return function(){ return f.call(a.concat(array.prototype.splice.all(arguments,0))); } }
Comments
Post a Comment