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's 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 I could implement exactly ONE of these gotos 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's 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's 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 both gotos. And, no, I don’t want additional flags.
EDIT:
@Luis Espinal: Your proposed solution doesn’t work. If info is {children: 'a'}, your program fails 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 seemed to think the fourth conditional is relevant to my problem. The problem is actually that I cannot do the following:
If condition1 Then action1
If !condition1 && condition2 Then action2
If !condition2 && condition3 && regardlessOf(condition1) Then action3
Without using flags (temporary boolean variables).
Basically if condition1 is true, I don’t have to test for condition2, and, if condition2 is true, I don’t have to test for condition3. But, if condition1 && !condition2, I might have to test for condition3.
Your function has a non planar control flow graph, and as such cannot be implemented using structured programming logic.
If you are concerned with speed, your best bet would be to duplicate
label 1and structure the code as follows: