One JavaScript function calling several others -
i know javascript function capable of calling other functions. i've utilized several times.
currently i'm working on script find html elements of tag, , add in bit of text based on kind of tag is.
function foo() { dealwithh1(); dealwithh2(); dealwithh3(); etc... } i have done few searches regarding functions , can't seem find definite, consistent answer whether or not practice. way, because there lot of function calls make, , keeps organized. want make sure before far there isn't glaring problem doing this. function foo() serves no purpose called @ onload , call of these other functions.
is okay, or there more encouraged way done?
it's reasonable create pseudo-dsl of operations composed of functions.
based on description of dealwith... functions do, though, think might want generalize functions more, like:
function foo() { taginsert('h1', 'text append h1 tag'); taginsert('h2', 'text append h2 tag'); taginsert('h3', 'text append h3 tag'); } now essence of function has been distilled (append text tags of specified type), , variables of function have been parameterized (the type of tag , text append), like:
var tags = { 'h1': { 'en': 'hello', 'es': 'hola', 'sr@latin': 'zdravo' }, 'h2': { 'en': 'goodbye', 'es': 'adios', 'sr@latin': 'do vidjenja' }, 'h3': { 'en': 'green', 'es'; 'verde', 'sr@latin': 'zelena' } }; function foo(locale) { for(var tag in tags) { taginsert(tag, tags[tag][locale]); } } composability of functions improved when operation perform deferred until function called -- can use exact same function not append fixed set of text tags, in whatever language user prefers use.
this flexibility can of course taken absurd lengths , you'd never done, it's think of function set operator: input function (declared explicitly variable or implicitly accessing global) , operation performed produce new output set?
if doesn't take effort write function in generic way versus specific case you're dealing with, write way , can re-use function whenever need similar action different inputs.
remember...
don't take taginsert definition @ face value, know barely anything you're trying do, , maybe generalization doesn't make sense. point you developer should have better idea of you're trying accomplish.
if follow larry wall's virtues of programmer, should trying minimize amount of work have do, , functions reach right degree of composability versus complexity.
functions calling functions whole point of function -- avoid need rewrite on , on again; dividing massive imperative declaration of actions perform series of functions not point of function. repetitive patterns in imperative code , how can lazy possible?
Comments
Post a Comment