The answer depends, for me, on many things. Some are:
- is it a prototype or not? Looks like it is
- are you in a rush? If so, do whatever works for you IF it's a prototype.
- how big will the whole project be?
Here's a list of things that bother me:
Function name: "from"
from? Might make sense to you but it's not a very good name. It's too...generic. I'd take swissArmyKnifeFromElementswissArmyKnifeFromElementover fromfromany day - easy to remember, hard to confuse. If this method is somehow part of an object (like your TreeTree), themthen it might be OK for a private function.The arguments might mean something else too - start
startand endendsuggest a range. It's potentially misleading. endendcan be many things (two, so far): a rule, like 'node''node'or 'child_list''child_list'or a class name. Again, misleading - it will only bring pain.function findTreeElements(root, rule, params) { //implementation }
//you could call it like this findTreeElements(a,'class',{'name':'someClassName'}); findTreeElements(a,'headline');
function findTreeElements(root, rule, params) { //implementation } //you could call it like this findTreeElements(a,'class',{'name':'someClassName'}); findTreeElements(a,'headline');The more your project grows, the more complex this function will become - in ways that you cannot foresee :) Maybe that's why your friend was talking about objects and multiple functions. jQuery uses objects everywhere and I think that you should do that too:
- define a tree/list object
- define a task/node/whatever object
- have the tree keep a list of tasks/nodes/etc
- define the methods for manipulating the tree/node/whatever inside the associated objects
something like (totally random, possibly related, example):
var list = tree.findNode(someNodeNameOrIndex).tasks();
It's up to TreeTree object to manage it'sits list of nodes and implement the findNodefindNode method (that parses the DOM whatever way you like).
It's up to the NodeNode object (returned by Tree's findNode) to manage the list of tasks inside that node (or something similar) and implement the tasks function which would work in a similar way to your current find(elem, 'child_list')find(elem, 'child_list').
Look at the code written by 'better' javascript developers and learn from them. There are many good projects out there that can teach you a lot. Look for decent sized projects, not 'plugins' or basic snippets; browser extensions are also good candidates.