-
-
Save evilbuck/4081178 to your computer and use it in GitHub Desktop.
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 the fun
.
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's tap
is all about - to intercept the call chain with the "current" object.
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.
I'd add the following to the usage: