Ruby Refactoring の本 (Fields、Harvie、Fowler) を読んでいます。中間部分が互いに異なるメソッドがある場合、重複を避けるために使用できる Extract Surrounding Method 操作について言及しています。
def number_of_descendants_named(name)
count_descendants_matchin { |descendant| descendant.name == name }
end
def number_of_living_descendants
count_descendants_matching { |descendant| descendant.alive? }
end
def count_descendants_mathing(&block)
children.inject(0) do |count, child|
count += 1 if yield child
count + child.count_descendants_matching(&block)
end
end
私はあなたがその考えを理解すると確信しています。Javascript でどのように同様のことを行いますか?