views:

298

answers:

1

Not sure if this is possible in Dojo or JS for a function to return a object with two properties of vars? My codes do not work but just as my requirement:

function getObj()
{
    var var1 = 123; // 123 as simple case, but it could be a DOM node
    var var2 = 345; // another DOM node
    return {v1: var1, v2: var2}; // any way to get two values back?
}
....
var obj = getObj();
console.log("obj.v1: " + obj.v1 + "; obj.v2: " + obj.v2); // not working but possible?

OK, here is my real function:

 dojo.requre("dojo.html");
 ....
 function getNodes()
 {
     var node = dojo.byId("div1"); // static <div> in html 
     var childen = "<div id='chart' style='width: 10px; height: 10px'></div><div id='legend'></div>";
     dojo.html.set(node, children); // add two divs as children
     var nodeChart = dojo.byId("chart");
     var nodeLegent = dojo.byId("legend");
     return {chart: nodeChart, legend: nodeLegend};
 }
 ...
 var nodes = getNodes();
 var nodeChart = nodes.chart; // OK div#chart
 var nodeLegend = nodes.legend; // nodes.legend is div#legend, but nodeLegend is undefined!
+5  A: 

Yes that is possible. And I just tried your code and it works fine...

<html>
  <head>
    <script>
        function getObj(){
          var var1 = 123; // 123 as simple case, but it could be a DOM node
          var var2 = 345; // another DOM node
          return {v1: var1, v2: var2}; // any way to get two values back?
        }

        var obj = getObj();
        alert(obj.v1);
    </script>
  </head>
  <body>
    <h1>Test</h1>
  </body>
</html>

EDIT:

You have a typo...

 var nodeLegent = dojo.byId("legend");
 return {chart: nodeChart, legend: nodeLegend};

Your var is named nodeLegent, but you are passing nodeLegend (undefined)

Don't feel bad; everybody makes these kind of mistakes. Simply requires another pair of eyes.

Josh Stodola
Hummm, interesting. I may have something wrong in my codes....
David.Chu.ca
Yes, that's what I suspect. You probably have a syntax elsewhere.
Josh Stodola