这个开关 returns 未定义的任何特殊原因?

Any particular reason why this switch returns undefined?

我是 JS 的新手,我一直在使用它只是为了好玩,因为我有空闲时间。我在网上看了几分钟手册后写了下面的代码,所以请不要怪我。

var doctype = document.type;
var n = doctype.indexOf("}") + 1;
var doctype2 = doctype.substring(n);
switch (doctype2) {
    case "doc_func_adm":
        var newtype = "documento-admissao-funcionario";
        switch (document.properties["ext1:tipo_doc_func_adm"]) {
            case "Contrato Experiência":
                var newtype2 = "contrato-experiencia";
                break;
            case "Prorrogação de Contrato":
                var newtype2 = "prorrogacao-contrato";
                break;
            case "Contrato: Prazo Indeterminado":
                var newtype2 = "contrato-prazo-indeterminado";
                break;
            case "Termo Bolsa Família":
                var newtype2 = "termo-bolsa-familia";
                break;
            case "Vale Transporte":
                var newtype2 = "vale-transporte";
                break;
            case "Ficha Registro":
                var newtype2 = "ficha-registro";
                break;
        } 
   break;
 }
 print(newtype + "-" + newtype2);
 print(doctype2); //check string result being tested

控制台结果:

 undefined-undefined
 doc_func_adm

请注意 'doctype2' 与开关测试具有相同的字符串。 如您所见,它 returns 只有未定义的值,我相信我遗漏了一些我看不到的非常基本的东西。

感谢您的帮助。干杯。

我原本以为你的问题是范围,但在阅读了@JohnnyMopp 对原始 post 的评论,并在 scope of javascript variables 上检查了这个 link - 我会说问题出在这里:

 var doctype2 = doctype.substring(n);

  switch (doctype2) {
     case "doc_func_adm": 
      var newtype = "documento-admissao-funcionario";

我认为 doctype2 从未遇到过 "doc_func_adm";

如果这种情况至少发生一次,变量 newtype 将包含 "documento-admissao-funcionario" 并且不会未定义。