简体   繁体   English

使用非拉丁字母作为 object 属性名称是一种不好的做法吗?

[英]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.在我的项目中,最简单的方法是使用 object,其中字母作为属性名称,发音作为相应的值。

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

because later I can get the pronunciation this way: return letters[georgianLetter] || ""因为以后我可以这样得到发音: 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?我知道任何字符串都可以用作 object 的键。但是使用非拉丁字母作为键名是不是一种不好的做法?

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. JavaScript 几乎从一开始就定义了标识符名称的有效字符,使用 Unicode 标准中的“标识符开始”和“标识符部分”的定义。 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 .您可以将 object 用于您正在做的事情,或者Map See MDN's Maps vs. Objects for several good points on which to choose in any given situation.请参阅 MDN 的Maps vs. Objects以了解在任何给定情况下可以选择的几个优点。 Since your object is unchanging, it would be fine, but then so would a Map .由于您的 object 是不变的,所以没关系,但是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 .签入 Unicode 字符属性实用程序非常容易,例如

I don't think this is an issue to use such character as an object key.我认为使用 object 键这样的字符不是问题。

As suggested in a comment, JavaScript has a dedicated tool to handle this use case.正如评论中所建议的那样,JavaScript 有一个专门的工具来处理这个用例。

As a better and safer alternative, using the JavaScript Map constructor to store your letters would look like this:作为更好更安全的替代方案,使用 JavaScript Map构造函数来存储您的信件如下所示:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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