Javascript 将大小写转换为字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14910760/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 18:18:07  来源:igfitidea点击:

Switch case as string

javascript

提问by kraYz

$(document).ready(function(){

    createForm("text,password",".content");  

});

function createForm(types,object){

    typ = types.split(',');

    //var source = "";

    $.each(typ,function(){

        switch(this){

            case "text":
            console.log('text');break;
            default: console.log('default');break;
        }


    });
    //$(object).html(source);
}

I have this code an in console it return 2xdefaults. Why?

我在控制台中有此代码,它返回 2xdefaults。为什么?

I try to return a input for each type as text or password but my switch does not recognize the "typ"

我尝试将每种类型的输入作为文本或密码返回,但我的交换机无法识别“类型”

采纳答案by T.J. Crowder

The reason you're seeing this behavior is that thiswithin the eachcall is a Stringobject instance, not a string primitive. JavaScript has both. In a switchstatement, the comparison with the cases is via ===, and a string instanceis not ===to a string primitive.

您看到这种行为的原因是thiseach调用中是一个String对象实例,而不是一个字符串基元。JavaScript 两者都有。在switch声明中,随着案件的比较是通过===和一个字符串的实例是不是===一个字符串原始

Three ways to fix it:

三种修复方法:

  1. If you change your switch to:

    switch (String(this)) {
    

    ...that will turn it back into a primitive, whereupon your switchworks.

  2. As VisioNpoints out in the comments below, use the arguments that $.eachpasses (each string — as a primitive — will be provided as the second argument):

    $.each(typ, function(index, value) {
        switch (value) {
            // ...
        }
    });
    
  3. Use any of the alternatives discussed in this other answer(one of which is a nice simple forloop).

  1. 如果您将开关更改为:

    switch (String(this)) {
    

    ...这将把它变成一个原始的,因此你的switch作品。

  2. 正如VisioN在下面的评论中指出的那样,使用$.each传递的参数(每个字符串——作为一个原语——将作为第二个参数提供):

    $.each(typ, function(index, value) {
        switch (value) {
            // ...
        }
    });
    
  3. 使用其他答案中讨论的任何替代方案(其中一个是一个很好的简单for循环)。



Side note: You're falling prey to The Horror of Implicit Globalsby not declaring your typvariable.

旁注:如果不声明变量,您就会成为隐式全局typ变量的牺牲品。

回答by TimWolla

jQuery is overkill here, for a correct way to do it with jQuery anyway have a look at T.J. Crowders answer.

jQuery 在这里有点矫枉过正,无论如何,使用 jQuery 的正确方法看看 TJ Crowders 的答案。

I recommend a method that is way simpler by using a standard for-loop. It works fine:

我推荐一种使用标准for循环更简单的方法。它工作正常:

var types = "text,password".split(",");
for (var i = 0; i < types.length; i++) {
    switch(types[i]){
        case "text":
            console.log('text');
        break;
        default: 
            console.log('default');
        break;
    }
}

回答by Tomá? Zato - Reinstate Monica

You use the $.eachfunction wrongly. It should look like this:

$.each错误地使用了该功能。它应该是这样的:

$.each( typ, function( key, value ) {
    switch(value){

       case "text":
            console.log('text');break;
       default:
            console.log('default');break;
    }
});

回答by Enrique Marcos

Try using switch(String(this))instead of switch(this). And of course, initialize your variables.

尝试使用switch(String(this))代替switch(this). 当然,初始化你的变量。