title: How can i tell if a function is being called as an argument to another function?
body: <p>javascript gurus! I realize the title to this question might be a little confusing. But I don't quite know how to explain it.</p>
  
  <p>So let me just give an example:</p>
  
  <pre><code>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";
  </code></pre>
  
  <p>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.</p>
  
  <p>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(). </p>
  
  <p>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.</p>
  
  <p>I'm starting to feel like this just isn't possible.</p>
  
  <p><strong>EDIT:</strong>
  so obviously most people are a little confused as to why i would want to do this.</p>
  
  <p>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</p>
  
  <pre><code>obj = c(tableElement,"td","td","td")
  </code></pre>
  
  <p>this will create 3 cells and append them to my table. but will not return anything. so that:</p>
  
  <pre><code>obj = undefined
  </code></pre>
  
  <p>but that's ok becuase if I change the original code to this:</p>
  
  <pre><code>obj = c(tableElement,"td","td",true,"td")
  </code></pre>
  
  <p>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.</p>
  
  <p>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.</p>
  
  <p>it all works good until I started adding the ability to nest elements. EX:</p>
  
  <pre><code>obj = c(tableElement,"tr",c("td",true,"td",true,"td",true),"tr",true)
  </code></pre>
  
  <p>this works well actually outputs:</p>
  
  <pre><code>&lt;table&gt;
     &lt;tr&gt;
         &lt;td&gt;&lt;/td&gt;
         &lt;td&gt;&lt;/td&gt;
         &lt;td&gt;&lt;/td&gt;
     &lt;/tr&gt;
     &lt;tr&gt;&lt;/tr&gt;
  &lt;/table&gt;
  </code></pre>
  
  <p>and obj = the second row element only.</p>
  
  <p>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.</p>
  
  <p>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.</p>
  
  <p>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.</p>