Javascript枚举到相应的字符串值 [英] Javascript Enum To Corresponding String Value

查看:1149
本文介绍了Javascript枚举到相应的字符串值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  var TEST_ERROR = {
'SUCCESS': 0,
'FAIL':-1,
'ID_ERROR':-2
};

并对网页上的功能执行测试:

  function test()
{
//从DOM
获取段落var testResultParagraph = document.getElementById('testResult') ;

//检查段落是否有效
if(!testResultBox)
{
//更新网页
testResultParagraph.value = TEST_ERROR.ID_ERROR ;
返回TEST_ERROR.ID_ERROR;
}

//存储结果的东西
var testResult = TEST_ERROR.SUCCESS;

//测试计算
testResult = testCalculate()

//更新网页
testResultParagraph.value = testResult;

//测试成功
返回TEST_ERROR.SUCCESS;
}

testCalculate()并且段落的值将为0,-1,-2取决于结果。



现在我想将其映射到一个字符串,以便该段落显示成功,失败或ID错误



我可以用几种方法来做到这一点:

  var TEST_ERROR = {
'SUCCESS':{VALUE:0,STRING:'Success'},
'FAIL':{VALUE: -1,STRING:'Fail'},
'ID_ERROR':{VALUE:-2,STRING:'Id Error'},
};

需要修改枚举点访问器,或

  var TEST_ERROR = {
'SUCCESS':0,
'FAIL':1,
'ID_ERROR':2
};

var TEST_STRING = [
'Success',
'Fail',
'ID Error'
];

哪些需要更改逻辑( result> TEST_ERROR.SUCCESS 似乎wierd tho!)



我的问题是你将如何将枚举值映射到JavaScript中的字符串值?强>我认为第二种方式是最明智的,但是希望枚举者对成功是积极的,否定失败。我也喜欢第一个包含对象结构中的字符串和值的想法。



任何想法?



谢谢!



Matt



PS。我将在网络工作者中进行测试,以使该页面不会挂起,结果将被放入表格中,而不是上面的段落。



PPS。我很喜欢Javascript编程,但在ASM,C,C ++,C#中做了很多。

解决方案

你真的需要数值吗?如果没有,你可以使用这样的东西:

  var TEST_ERROR = {
SUCCESS:'Success',
FAIL:'Fail',
ID_ERROR:'ID Error'
};


So I have this in the javascript for my page:

var TEST_ERROR  = {
        'SUCCESS'   :   0,
        'FAIL'      :   -1,
        'ID_ERROR'  :   -2
      };

And perform tests on functions in the page like so:

function test()
{
    // Get the paragraph from the DOM
    var testResultParagraph = document.getElementById('testResult');

    // Check the paragraph is valid
    if(!testResultBox)
    {
        // Update the web page
        testResultParagraph.value = TEST_ERROR.ID_ERROR;
        return TEST_ERROR.ID_ERROR;
    }

    // Something to store the results
    var testResult = TEST_ERROR.SUCCESS;

    // Test the calculation
    testResult = testCalculate()

    // Update the web page
    testResultParagraph.value = testResult;

    // The test succeeded
    return TEST_ERROR.SUCCESS;
}

The result of testCalculate() and the value of the paragraph will be either 0, -1, -2 depending on the outcome.

Now I want to map this to a string so that the paragraph shows 'Success', 'Fail' or 'ID Error'

I could do this a few ways I have figured:

var TEST_ERROR  = {
        'SUCCESS'   :   {VALUE : 0 , STRING: 'Success' },
        'FAIL'      :   {VALUE : -1, STRING: 'Fail'    },
        'ID_ERROR'  :   {VALUE : -2, STRING: 'Id Error'},
      };

would require a modification to the enum dot accessors, or

var TEST_ERROR  = {
        'SUCCESS'   :   0,
        'FAIL'      :   1,
        'ID_ERROR'  :   2
      };

var TEST_STRING = [
        'Success',
        'Fail',
        'ID Error'
      ];

Which would require changes to the logic (result > TEST_ERROR.SUCCESS seems wierd tho!)

My question is how would you go about mapping an enumerator value to a string value in Javascript? I'm thinking the second way is the most sensible, but would like the enumerator to be positive for successes and negative for fails. I also like the idea of the first containing the strings and values in the object structure.

Any ideas?

Thanks!

Matt

PS. I'm going to be doing the testing in a Web Worker, so that the page doesn't hang and the results will be put into a table, not a paragraph like above.

PPS. I'm pretty new to Javascript programming, but do a lot in ASM, C, C++, C#.

解决方案

Do you actually need the numeric values at all? If not then you could use something like this:

var TEST_ERROR  = {
    SUCCESS  : 'Success',
    FAIL     : 'Fail',
    ID_ERROR : 'ID Error'
};

这篇关于Javascript枚举到相应的字符串值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆