简体   繁体   中英

Is it a bad practice to use non-latin letters as object property names?

I need somehow store Georgian letters and their pronunciation, and later return the pronunciation for a givven letter. The easiest way to do that in my project is to use an object with the letters as property name and pronunciations as corresponding values.

const letters = {
    ბ: 'b',
    დ: 'd',
    ს: 's',
    წ: 'ts',
    // and so on
}

because later I can get the pronunciation this way: return letters[georgianLetter] || "" return letters[georgianLetter] || ""

I know any string could be used as a key for an object. But is it a bad practice to use non-latin letters for key names?

I have no idea how else I can implement this and still keep the code simple. I tried to use an array:

const letters = [
    ['ბ', 'b'],
    ['დ', 'd'],
    ['ს', 's'],
    ['წ', 'ts'],
    // and so on
]

and then get the value like this:

return letters.find(item => item[0] === georgianLetter)[1] || ""

but it's a bit more complex solution and objects are preferred in this case since they make code shorter and cleaner.

But is it a bad practice to use non-latin letters for key names?

Not at all. JavaScript has defined the valid characters for identifier names inclusively almost since inception, using the definition of an "identifier start" and an "identifier part" from the Unicode standard. That means you don't even have to use bracket notation and strings for Georgian letters¹ as you would for some other characters (like - or / ), you can reliably use property literals as you have in your example:

 const letters = { ბ: "b", დ: "d", ს: "s", წ: "ts", // and so on }; console.log(letters.ბ); console.log(letters.დ); console.log(letters.ს); console.log(letters.წ);

You can use an object for what you're doing, or alternatively a Map . See MDN's Maps vs. Objects for several good points on which to choose in any given situation. Since your object is unchanging, it would be fine, but then so would a Map .

 const letters = new Map([ ["ბ", "b"], ["დ", "d"], ["ს", "s"], ["წ", "ts"], // and so on ]); console.log(letters.get("ბ")); console.log(letters.get("დ")); console.log(letters.get("ს")); console.log(letters.get("წ"));


¹ I'll admit I'm assuming here they're all classified as "identifier start." It's easy enough to check in the Unicode Character Properties Utility, for instance for .

I don't think this is an issue to use such character as an object key.

As suggested in a comment, JavaScript has a dedicated tool to handle this use case.

As a better and safer alternative, using the JavaScript Map constructor to store your letters would look like this:

 const letters = new Map([ ['ბ', 'b'], ['დ', 'd'], ['ს', 's'], ['წ', 'ts'] ]); console.log(letters.get('ბ'));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM