Last active
December 12, 2015 12:39
-
-
Save MaxMotovilov/4773252 to your computer and use it in GitHub Desktop.
Property, method, slot binders: eager and late
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // One-way property binders | |
| function bindNowGetProp( propname, obj ) { | |
| return function() { return obj[propname]; } | |
| } | |
| function bindNowSetProp( propname, obj ) { | |
| return function(v){ return obj[propname] = v; } | |
| } | |
| function bindLaterGetProp( propname ) { | |
| return function( obj ) { | |
| return obj[propname]; | |
| } | |
| } | |
| function bindLaterSetProp( propname ) { | |
| return function( obj, v ) { | |
| return obj[propname] = v; | |
| } | |
| } | |
| // Generic one-way property binders | |
| function bindGetProp( propname, obj ) { | |
| return arguments.length < 2 | |
| ? bindLaterGetProp( propname ) : | |
| : bindNowGetProp( propname, obj ); | |
| } | |
| function bindSetProp( propname, obj ) { | |
| return arguments.length < 2 | |
| ? bindLaterSetProp( propname ) : | |
| : bindNowSetProp( propname, obj ); | |
| } | |
| // Slot binders | |
| function bindNowSlot( propname, obj ) { | |
| return function(v) { | |
| return arguments.length<1 ? obj[propname] : obj[propname] = v; | |
| } | |
| } | |
| function bindLaterSlot( propname ) { | |
| return function(obj,v) { | |
| return arguments.length<2 ? obj[propname] : obj[propname] = v; | |
| } | |
| } | |
| // Generic slot binder | |
| function bindSlot( propname, obj ) { | |
| return arguments.length<2 | |
| ? bindLaterSlot( propname ) | |
| : bindNowSlot( propname, obj ); | |
| } | |
| // Method binders (bindNowMethod only shown for completeness sake) | |
| function bindNowMethod( propname, obj ) { | |
| return obj[propname].bind( obj ); | |
| } | |
| function bindLaterMethod( propname ) { | |
| return function( obj ) { | |
| return obj[propname].apply( obj, Array.prototype.slice.call( arguments, 1 ) ); | |
| } | |
| } | |
| // Generic method binder | |
| function bindMethod( propname, obj ) { | |
| return arguments.length<2 | |
| ? bindLaterMethod( propname ) | |
| : bindNowMethod( propname, obj ); | |
| } | |
| // Technical: arg-list repackager | |
| function appendArgs( prefix, args, from ) { | |
| Array.prototype.push.apply( prefix, Array.prototype.slice.call( args, from ) ); | |
| return prefix; | |
| } | |
| // Re-bind to specific object and optional arguments (equivalent of Function.bind) | |
| function bindNow( latebound /*...*/ ) { | |
| return Function.prototype.bind.apply( latebound, appendArgs( [null], arguments, 1 ) ); | |
| } | |
| // Implementation of Eugene's lateBind() | |
| function lateBind( object, name /*...*/ ) { | |
| return bindNow.apply( null, appendArgs( [ bindLaterMethod( name ), object ], arguments, 2 ) ); | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment