Created
November 15, 2012 20:50
-
-
Save evilbuck/4081178 to your computer and use it in GitHub Desktop.
javascript implementation of Ruby 1.9's tap
This file contains 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
Object.defineProperty(Object.prototype, 'tap', { | |
value: function(fun){ | |
fun.call( this ); | |
return this; | |
}, | |
enumerable: false | |
}); | |
// Usage: | |
// a = []; | |
// a.tap(function(){ this.push('foo'); }); | |
// a => [ 'foo' ] |
I'd add the following to the usage:
b = []; b.tap(function(x){ x.push('foo'); }); // b => [ 'foo' ]I don't believe that would actually work with this implementation.
x
would be undefined as there are not any arguments passed to thefun
.You're right. I mistakenly thought it also accepts the object itself as an argument.
But I think it should have this argument, as this is what Ruby'stap
is all about - to intercept the call chain with the "current" object.
Ahh. Yes. I think I remember implementing it incorrectly after the fact when I typed this up 8 years ago. Thanks for the feedback. I'll need to look up how Ruby tap works again. I haven't used ruby in about 5 years.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You're right. I mistakenly thought it also accepts the object itself as an argument.
But I think it should have this argument, as this is what Ruby's
tap
is all about - to intercept the call chain with the "current" object.