First off,
I read the related articles on this and felt they were unsuitable to me since I am using a series of methods I am inheriting and not a few arithmetic functions I can easily change. Secondly, googling 'javascript chaining method'
has results mostly concerning jQuery and D3. The JavaScript chain
method is surprisingly under-documented it seems.
I am trying to implement underscore in CoffeeScript. I am working on the _.chain
method. I am decorating underscore methods to newObject
and then returning it. I have a private variable called value
which is suppose to be the value that is modified by chained method calls. Calling getValue()
on the returned newObject
should return the private value
variable.
For some reason though the private value
variable is not changing when I call a method on newObject
. The underscore methods are being curried correctly, but they do not change when a function is invoked. I tried reading the underscore source code but it seems they architect there chain method in a completely different way that I cannot duplicate without restructuring the way my underscore object is constructed.
_.chain
_.chain = (v) ->
newObj = {}
value = v
newObj.getValue = -> value
for name, fn of _
if typeof fn is 'function'
newObj[name] = (callback, otherVal) ->
value = fn(value, callback, otherVal)
newObj
newObj
Example:
var a = _.chain([1,2,3,4]);
a.map(function(value) {
return value + 5;
})
a.getValue().getValue()
>>>[1, 2, 3, 4]