javascript gurus! I realize the title to this question might be a little confusing. But I don't quite know how to explain it.
So let me just give an example:
function foo(txt){
return "Hello " + txt);
}
function bar(txt){
if(/* bar is called as an argument */) return txt;
if(/* bar is called normally */) return "Bar";
}
foo(bar("world!")); //Returns "Hello World";
bar("world!"); //Returns "Bar";
I want bar() to do something differently if it's called as an argument for another function than as if it were called on it's own.
I can't seem to find any way for bar() to know if it's an argument in another function or not because it seems that when the browser runs foo(bar("world!")); it runs bar("world!") first and then gives foo() whatever was the result of bar().
I thought i could check the call stack for bar() because I figured that foo() was calling it. but it's being called by the same function that called foo() before foo() ever gets called.
I'm starting to feel like this just isn't possible.
EDIT: so obviously most people are a little confused as to why i would want to do this.
I'm making myself a function for quickly building HTML elements. I can call the function and just pass in the parent element i want them to attach to and a bunch of tag names and it creates those tags and appends them to that parent element
obj = c(tableElement,"td","td","td")
this will create 3 cells and append them to my table. but will not return anything. so that:
obj = undefined
but that's ok becuase if I change the original code to this:
obj = c(tableElement,"td","td",true,"td")
then it will do the same thing. but instead pass the second cell to obj so that i can easily use that cell again later.
it comes in handy when i want to use Javascript to build a bunch of stuff that most of it I don't need to manipulate afterwards.
it all works good until I started adding the ability to nest elements. EX:
obj = c(tableElement,"tr",c("td",true,"td",true,"td",true),"tr",true)
this works well actually outputs:
<table>
<tr>
<td></td>
<td></td>
<td></td>
</tr>
<tr></tr>
</table>
and obj = the second row element only.
Now the trick that I'm trying to accomplish with all that is. I don't want to need the TRUE arguments in my nested c() function. they aren't necessary because they are required no matter what. If I had some way of knowing it was nested. then I could just ignore the TRUE arguments and my code would be that much more concise.
I'd also like to be able to have one of those nested cells be able to be returned to the obj variable as well.
I'm thinking my only option is to create a second function that works just a little bit differently for being used as an embedded element creator.