How could I emulate the goto programming construct in this case?
$.fn.hierarchy = function(info, ret) { if (info.constructor !== Object) { info = {children: info}; goto label1; // Illegal JavaScript } if (!info.children) { info.children = []; goto label2; // Illegal JavaScript } label1: if (info.children.constructor !== Array) info.children = [info.children]; label2: /* // Forget this code. It irrelevant to my specific problem // (which is that JS doens't allow non-nested conditionals) // and caused much confusion. if (!info.tagc) info.tagc = info.tag || 'div'; */
I know that I can implement ONE of these goto as an else clause:
$.fn.hierarchy = function(info, ret) { if (info.constructor !== Object) { info = {children: info}; //goto label1; } else if (!info.children) { info.children = []; goto label2; // Illegal JavaScript } //label1: if (info.children.constructor !== Array) info.children = [info.children]; label2: /* // Forget this code. It irrelevant to my specific problem // (which is that JS doens't allow non-nested conditionals) // and caused much confusion. if (!info.tagc) info.tagc = info.tag || 'div'; */
Or:
$.fn.hierarchy = function(info, ret) { if (info.constructor !== Object) { info = {children: info}; goto label1; // Illegal JavaScript } if (!info.children) { info.children = []; //goto label2; } else { label1: if (info.children.constructor !== Array) info.children = [info.children]; } //label2: /* // Forget this code. It irrelevant to my specific problem // (which is that JS doens't allow non-nested conditionals) // and caused much confusion. if (!info.tagc) info.tagc = info.tag || 'div'; */
But I want to have like goto s. And no, I do not want additional flags.
EDIT:
@Luis Espinal: Your proposed solution does not work. If info is {children: 'a'} , your program will not be able to convert info.children to [a] .
$.fn.hierarchy = function(info, ret) { if (info.constructor !== Object) { info = {children: info}; // goto label1; // Illegal JavaScript // label1: if (info.children.constructor !== Array){ info.children = [info.children]; } } else if (!info.children) { info.children = []; // goto label2; // Illegal JavaScript // label2: /* // Wrong. This has to be placed outside all of this. if (!info.tagc) { info.tagc = info.tag || 'div'; } */ } /* the following code is missing: else { // Handles the case when info.constructor === Object // from the beginning // AND // info.children isn't an array if (info.children.constructor !== Array) info.children = [info.children]; } */
EDIT: Some of you seem to have thought that the fourth condition is relevant to my problem. The problem is that I cannot do the following:
If condition1 Then action1 If !condition1 && condition2 Then action2 If !condition2 && condition3 && regardlessOf(condition1) Then action3
Without the use of flags (temporary Boolean variables).
Basically, if condition1 true, I don't need to check for condition2 , and if condition2 true, I don't need to test condition3 . But, if condition1 && !condition2 , I may have to test for condition3 .