Как я мог бы эмулировать программную конструкцию goto
в этом случае?
$.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';
*/
Я знаю, что мог бы точно реализовать ONE из этих goto
s как условие else:
$.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';
*/
Или:
$.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';
*/
Но я хочу иметь оба goto
с.И нет, мне не нужны дополнительные флаги.
РЕДАКТИРОВАТЬ:
@ Луис Эспиналь: Ваше предлагаемое решение не работает.Если info
равно {children: 'a'}
, ваша программа не может преобразовать info.children
в [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];
}
*/
РЕДАКТИРОВАТЬ: Некоторые из вас, кажется, думают четвертыйусловно относится к моей проблеме.На самом деле проблема в том, что я не могу сделать следующее:
If condition1 Then action1
If !condition1 && condition2 Then action2
If !condition2 && condition3 && regardlessOf(condition1) Then action3
Без использования флагов (временных логических переменных).
В основном, если condition1
- true, мне не нужно проверятьдля condition2
, и, если condition2
истинно, мне не нужно проверять condition3
.Но, если condition1 && !condition2
, мне, возможно, придется проверить на condition3
.